gpt4 book ai didi

java - Hibernate - 与 2 @OneToMany 一起崩溃

转载 作者:行者123 更新时间:2023-12-02 10:52:40 25 4
gpt4 key购买 nike

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

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

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

表用户:

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 中的一个条目,一切就可以正常工作。一旦我添加这两个条目, hibernate 就会崩溃并且没有错误消息。是否无法在一个实体中执行两个 @OneToMany 语句?

最佳答案

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

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

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

关于java - Hibernate - 与 2 @OneToMany 一起崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52038471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com