I am intrigued by the combination of QueryDSL/Spring-data-dpa/Hibernate, but I must not quite get it yet as even a simple example doesn't work for me. If there an online tutorial or documentation that would point me in the right direction, I would appreciate the link because so far I haven't found it. Here is my example:
Spring 3.1.1
Hibernate 4.0.1FINAL
QueryDSL 2.7.0
Two tables without a foreign key between them.
Two entities:
Code:@Entity @Table(name = "User", uniqueConstraints = @UniqueConstraint(columnNames = "UserName")) public class User { // Fields private Long userId; private Integer status = 0; private String password; private String userName; private UserRole userRole; @Id @Column(name = "UserId", unique = true, nullable = false) @GeneratedValue(strategy = GenerationType.IDENTITY) public Long getUserId() { return this.userId; } @OneToOne @JoinColumn(name = "userRoleId") public UserRole getUserRole() { return userRole; } ... }I have two Repositories interfaces:Code:@Entity @Table(name = "UserRole") public class UserRole { // Fields private Long userRoleId; private String userRoleDescriptionShort; private String userRoleDescription; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) // @PrimaryKeyJoinColumn public Long getUserRoleId() { return this.userRoleId; } ... }
Code:@Repository public interface IUserRepository extends JpaRepository<User, Long> { public User findByUserName(String userName); }and two implementations:Code:public interface IUserRoleRepository extends JpaRepository<UserRole, Long> { public UserRole findByRoleDescriptionShort(String roleDescriptionShort); }
Code:@Repository @Transactional(readOnly = true) public class UserRepository extends QueryDslRepositorySupport implements IUserRepository { @PersistenceContext private EntityManager em; @Override public User findByUserName(String userName) { QUser user = QUser.user; User auser = (User) from(user).where(user.userName.eq(userName)); return auser; } @Repository @Transactional(readOnly = true) public class UserRoleRepository extends QueryDslRepositorySupport implements IUserRoleRepository { @PersistenceContext private EntityManager em; @Override public UserRole findByRoleDescriptionShort(String roleDescriptionShort) { QUserRole userRole = QUserRole.userRole; UserRole arole = (UserRole) from(userRole).where(userRole.userRoleDescriptionShort.equalsIgnoreCase("Owner")); return arole; }
However, when I try to build I get this:
Caused by: java.lang.IllegalArgumentException: No property role found for type class com.edelweissco.model.people.UserRole
at org.springframework.data.repository.query.parser.P roperty.<init>(Property.java:76)
at org.springframework.data.repository.query.parser.P roperty.<init>(Property.java:97)
at org.springframework.data.repository.query.parser.P roperty.create(Property.java:312)
at org.springframework.data.repository.query.parser.P roperty.create(Property.java:326)
at org.springframework.data.repository.query.parser.P roperty.create(Property.java:326)
at org.springframework.data.repository.query.parser.P roperty.create(Property.java:292)
at org.springframework.data.repository.query.parser.P roperty.from(Property.java:251)
at org.springframework.data.repository.query.parser.P roperty.from(Property.java:232)
at org.springframework.data.repository.query.parser.P art.<init>(Part.java:48)
at org.springframework.data.repository.query.parser.P artTree$OrPart.<init>(PartTree.java:242)
at org.springframework.data.repository.query.parser.P artTree.buildTree(PartTree.java:101)
at org.springframework.data.repository.query.parser.P artTree.<init>(PartTree.java:77)
at org.springframework.data.jpa.repository.query.Part TreeJpaQuery.<init>(PartTreeJpaQuery.java:56)
at org.springframework.data.jpa.repository.query.JpaQ ueryLookupStrategy$CreateQueryLookupStrategy.resol veQuery(JpaQueryLookupStrategy.java:92)
at org.springframework.data.jpa.repository.query.JpaQ ueryLookupStrategy$CreateIfNotFoundQueryLookupStra tegy.resolveQuery(JpaQueryLookupStrategy.java:159)
at org.springframework.data.jpa.repository.query.JpaQ ueryLookupStrategy$AbstractQueryLookupStrategy.res olveQuery(JpaQueryLookupStrategy.java:71)
at org.springframework.data.repository.core.support.R epositoryFactorySupport$QueryExecutorMethodInterce ptor.<init>(RepositoryFactorySupport.java:303)
at org.springframework.data.repository.core.support.R epositoryFactorySupport.getRepository(RepositoryFa ctorySupport.java:157)
at org.springframework.data.repository.core.support.R epositoryFactoryBeanSupport.getObject(RepositoryFa ctoryBeanSupport.java:120)
at org.springframework.data.repository.core.support.R epositoryFactoryBeanSupport.getObject(RepositoryFa ctoryBeanSupport.java:39)
at org.springframework.beans.factory.support.FactoryB eanRegistrySupport.doGetObjectFromFactoryBean(Fact oryBeanRegistrySupport.java:142)
As I understand it, spring-data-jpa means I don't have to provide an implementation of the Repository. But because I want to use QueryDSL, I need to write the implementation?
Thank you for any assistance, I have been stumped on this and can't seem to find the solution.


Reply With Quote