【问题标题】:Hibernate - Crash with 2 @OneToMany休眠 - 与 2 @OneToMany 崩溃
【发布时间】:2019-02-01 22:36:50
【问题描述】:

在我的 Users.entity 中使用多个 @OneToMany 条目时,Hibernate 崩溃,我不明白为什么。

我有一个用户表(主键 userID)和其他各种表,它们通过数据库中设置的外键(InnoDB 集和外键在每个依赖表中设置)引用主键 userID。

这里是一个包含三个表的示例:

表用户:

CREATE TABLE `users` (`userID` int(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1; 
ALTER TABLE `users` ADD PRIMARY KEY (`userID`);

表:假期请求

CREATE TABLE `vacationrequests` (`requestID` int(11) NOT NULL,`userID` int(4) NOT NULL,) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `vacationrequests` ADD CONSTRAINT `userID` FOREIGN KEY (`userID`) REFERENCES `users` (`userID`) ON UPDATE CASCADE; COMMIT;

表月结:

CREATE TABLE `monthend` (`monthendID` int(11) NOT NULL,`userID` int(4) NOT NULL,) ENGINE=InnoDB DEFAULT CHARSET=latin1;

实体用户:

@Entity
@Table(name="users")

public class Users {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="userID")
private int userID;

... .... (other variables)

@OneToMany(fetch = FetchType.LAZY, mappedBy="monthend")
private Set<Monthend> monthend;

@OneToMany(fetch = FetchType.LAZY, mappedBy="vacationrequests")
private Set<Vacationrequests> vacationrequests;

public Users() {

}

实体假期请求:

@Entity
@Table(name="vacationrequests")

public class Vacationrequests {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="requestID")
private int requestID;

... .... (other variables)

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="userID", nullable = false)
private Users users;

public Vacationrequests() {

}

实体月结:

@Entity
@Table(name="monthend")

public class Monthend {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="monthendID")
private int monthendID;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="userID", nullable = false)
private Users users;

@Column(name="clientID")
private int clientID;

@Column(name="month")
private int month;

@Column(name="year")
private int year;

@Column(name="monthend")
private int monthend;

public Monthend() {

}

工作查询:

List<Object> listJoinUsersMonthend = session.createQuery("from Monthend m inner join m.users as users where m.clientID = :clientID AND m.year = :year AND users.loginclass = 0 AND users.isactive = 1 AND m.monthend = 0 AND m.month < :month order by users.userID asc")
                        .setInteger("clientID", user.getClientID())
                        .setInteger("year", year)
                        .setInteger("month", month)
                        .getResultList();           

我想整合第二个查询:

List<Object> listJoinVacationrequestsUsers = session.createQuery("from Vacationrequests v inner join v.users as users where v.clientID = :clientID AND v.approved=0 AND v.vacationtype=1 AND YEAR(v.startdate) = :year" )
                        .setInteger("clientID", user.getClientID())
                        .setInteger("year", year)
                        .getResultList();       

只需在 Users.entity 中进行一个查询和一个条目,一切都可以正常工作。一旦我添加了这两个条目,休眠就会崩溃并且没有错误消息。不能在一个实体中做两个@OneToMany 语句吗?

【问题讨论】:

    标签: java sql hibernate


    【解决方案1】:

    似乎问题在于用户实体中的关联映射。 您应该更改关联的 ma​​ppedBy 属性以指定引用关联中当前实体的字段名称(在本例中为 users),如下所示:

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "users")
    private Set<Monthend> monthend;
    
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "users")
    private Set<Vacationrequests> vacationrequests;
    

    【讨论】:

      猜你喜欢
      • 2021-08-21
      • 2013-07-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2015-07-07
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多