我一直试图解决这个问题很长一段时间没有成功。我在用:
- 春天 3.1.1
- 休眠 4.0.1.Final
- spring-data-jpa 1.0.2.RELEASE
- MySQL 5 (org.hibernate.dialect.MySQL5InnoDBDialect)
我有两个实体,具有 OneToOne 非定向关联。表之间没有外键关系
@Entity
@Table(name = "User", uniqueConstraints = @UniqueConstraint(columnNames = "UserName"))
public class User {
// Fields
private Long userId;
private Integer status = 0;
private String password;
private Long userRoleId;
private String userName;
private UserRole userRole;
...
@OneToOne
@JoinColumn(name = "userRoleId")
public UserRole getUserRole() {
return userRole;
}
}
和另一个我不想被级联删除/任何东西作为参考的类
@Entity
@Table(name = "UserRole")
public class UserRole {
// Fields
private Long userRoleId;
private String userRoleDescriptionShort;
private String userRoleDescription;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getUserRoleId() {
return this.userRoleId;
}
}
每个表都有 userRoleId 声明为 bigint
我有两个存储库:
@Repository
public interface IUserRepository extends CrudRepository<User, Long> {
public User findByUserName(String userName);
}
@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;
}
和
@Transactional(readOnly = true)
public interface IUserRoleRepository extends CrudRepository<UserRole, Long> {
public UserRole findByRoleDescriptionShort(String roleDescriptionShort);
}
@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;
}
但是,当我构建它时,出现以下错误:
Caused by: java.lang.IllegalArgumentException: No property role found for type class com.edelweissco.model.people.UserRole
at org.springframework.data.repository.query.parser.Property.<init>(Property.java:76)
at org.springframework.data.repository.query.parser.Property.<init>(Property.java:97)
at org.springframework.data.repository.query.parser.Property.create(Property.java:312)
at org.springframework.data.repository.query.parser.Property.create(Property.java:326)
at org.springframework.data.repository.query.parser.Property.create(Property.java:326)
at org.springframework.data.repository.query.parser.Property.create(Property.java:292)
at org.springframework.data.repository.query.parser.Property.from(Property.java:251)
at org.springframework.data.repository.query.parser.Property.from(Property.java:232)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:48)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:242)
at org.springframework.data.repository.query.parser.PartTree.buildTree(PartTree.java:101)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:77)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>
(PartTreeJpaQuery.java:56)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:92)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:159)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:71)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:303)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:157)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:120)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:39)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
这似乎是一种非常简单的映射方式,但我看到的所有示例都是双向的。不过,我不明白为什么这不起作用。