【问题标题】:hibernate @ManyToOne no table relationshiphibernate @ManyToOne 没有表关系
【发布时间】:2012-09-29 14:05:28
【问题描述】:

目前使用:

  • 休眠 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;
}

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 和瞬态变量有关,但这里不是这种情况。

【问题讨论】:

  • User 表中 long 类型的 userRole 列是什么?它应该包含什么?你为什么不放弃它并用 UserRole.userRoleId 的外键替换它,因为这是你想要的?
  • 我为 userRoleId 设置了 long 值,认为 Hibernate 会将它们与该值相关联。当我使用 Hibernate 创建数据库时,它使 User.userRole 为 blob 类型,然后报错列太小。
  • 它应该与目标表中的 userRoleId 具有相同的类型,因为它应该包含相同的东西。
  • 编辑为在数据库中使用外键。

标签: spring hibernate querydsl


【解决方案1】:

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

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

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

应该简化为:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2021-11-14
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多