1

目前使用:

  • 休眠 4.0.1.Final
  • Spring-data-jpa:1.0.3.RELEASE
  • 查询DSL:2.3.0
  • MySQL 5.x

我有一个有趣的问题,我还没有找到答案或线索。我有两个没有外键或其他关系的表。但是为了尝试解决这个问题,我添加了一个。我希望我的用户实体持有它的用户角色。这种模式在整个数据库中重复出现,但这是最容易描述的。
这是我的表:

用户

userId           bigint(20) PK
password         varchar(255)
status           int(11)
userName         varchar(255)
userRoleId       long


CONSTRAINT `FK_USERROLE` FOREIGN KEY (`userRoleId`) REFERENCES `UserRole` (`userRoleId`)

用户角色

userRoleId       bigint(20) PK
userRoleDescription varchar(255)
userRoleDescriptionShort varchar(255)

这是我的课程: User.java

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@XmlRootElement(name = "User")
public class User  {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long userId;
private String password;
private int status;
private String userName;

@ManyToOne
@JoinColumn(name = "userRoleId")
private UserRole userRole;
public UserRole getUserRole() {
    return userRole;
}

public void setUserRole(UserRole userRole) {
    this.userRole = userRole;
}

用户角色.java

@Entity
@XmlRootElement(name = "userRole")
public class UserRole {
private Long userRoleId;
private String userRoleDescription;
private String userRoleDescriptionShort;

@ElementCollection
@OneToMany(mappedBy = "userRole")
private List<User> users;

public UserRole() {...}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getUserRoleId() {...    }

@OneToMany(mappedBy = "userRole")
public List<User> getUsers() {...}

因此,您可以看到我试图将 UserRole.userRoleId 与用户相关联的位置。我想也许 Hibernate 会在用户更新时构建映射并检索/关联 UserRole。

我已经回去并编辑了这篇文章以在表之间使用外键,但是在应用服务器启动时我得到了这个:

Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: UserRole, for columns: [org.hibernate.mapping.Column(users)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:304)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:288)
at org.hibernate.mapping.Property.isValid(Property.java:216)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:467)
at org.hibernate.mapping.RootClass.validate(RootClass.java:268)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1287)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1729)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)

我查找了该错误,该错误似乎与 JPA 和瞬态变量有关,但此处并非如此。

4

1 回答 1

0

如果我是你,我会先清理注释,禁止在同一个实体中同时注释 getter 和字段,这可能会导致意想不到的结果......

@ElementCollection
@OneToMany(mappedBy = "userRole")
private List<User> users;

@OneToMany(mappedBy = "userRole")
public List<User> getUsers() {...}

应简化为:

@ElementCollection
@OneToMany(mappedBy = "userRole")
public List<User> getUsers() {...}
于 2012-11-23T17:30:45.643 回答