Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How do I create and instantiate a jpa repository inside a class? I'm in a situation where I have to create repositories for different entities inside a generic class.

I could do that easily for Neo4j repositories like,

GraphRepository<T> graphRepository;

this.neo4jTemplate = new Neo4jTemplate(new RestGraphDatabase(
    "http://localhost:7474/db/data"));
this.graphRepository = neo4jTemplate.repositoryFor(domainClass); 

For JpaRepository, I checked the documentation and found this,

RepositoryFactorySupport factory = … // Instantiate factory here
UserRepository repository = factory.getRepository(UserRepository.class);

I'm not sure how to instantiate factory in the above code.

Also Can't I create repository like I did for Neo4j, by specifying the domain class?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
131 views
Welcome To Ask or Share your Answers For Others

1 Answer

I finally got it working this way,

SimpleJpaRepository<User, Serializable> jpaRepository;
jpaRepository = new SimpleJpaRepository<User, Serializable>(
    User.class, entityManager);

With SimpleJpaRepository, I can use all repository methods.

jpaRepository.save(user);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...