0

我一直试图解决这个问题很长一段时间没有成功。我在用:

  • 春天 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)

这似乎是一种非常简单的映射方式,但我看到的所有示例都是双向的。不过,我不明白为什么这不起作用。

4

1 回答 1

1

这是一个迟到的答案,但我不同意你的映射。您已经声明了一个带有 JoinColumn 的 OneToOne,指定 User 上有一个名为 userRoleId 的列,它是 UserRole 的外键。因此必须有一个外键。

另外,我不相信这是一对一的关系。您已声明在删除用户时不应级联删除 UserRole。听起来 UserRole 可以与任意数量的用户相关联。因此,用户上的映射应该是多对一。

OneToOne 通常适用于两个表共享相同键的情况,即使用 PrimaryKeyJoinColumn 表示它们共享一个主键。在这种情况下,两个实体具有不同的键。

于 2012-11-26T02:31:24.763 回答