hibernate @ManyToOne没有表关系

时间:2012-09-29 14:05:28

标签: spring hibernate querydsl

目前正在使用:

  • Hibernate 4.0.1.Final
  • Spring-data-jpa:1.0.3.RELEASE
  • QueryDSL:2.3.0
  • MySQL 5.x

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

用户

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


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

的UserRole

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;
}

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与User关联的位置。我想也许Hibernate会在用户更新时构建映射并检索/关联UserRole。

我已经回过头来编辑这篇文章,在表之间使用外键,但是在app server startup上我得到了这个:

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和瞬态变量有关,但这并非如此。

1 个答案:

答案 0 :(得分:0)

如果我是你,我会首先清理注释,禁止在同一个实体中注释BOTH getter AND字段,它可能最终导致意外结果...

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

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

应简化为:

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