简体   繁体   中英

Hibernate - Crash with 2 @OneToMany

Hibernate crashes when using more than one @OneToMany entries in my Users.entity and I don't understand why.

I have a table for users (primary key userID) and various other tables which refer to the primary key userID by a foreign key set in database (InnoDB set and foreign key is set in each depending table).

Here an example with three tables:

Table users:

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

Table: vacationrequests

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;

Table monthend:

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

Entity Users:

@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 Vacationrequests:

@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 Monthend:

@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() {

}

Working Query:

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();           

This second query i would like to integrate:

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();       

Everything works fine with just one query and one entry in the Users.entity. As soon as I add the two entries hibernate just crashes and there is no error message. Is it not possible to do two @OneToMany statements in a entity ?

It seems that the problem is in the mapping of the associations in the Users entity. You should change the mappedBy attribute of the associations to specify the field name that references the current entity in the associated one (in this case users ), like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM