问题
I am having an issue with a hibernate mapper while trying to store items within the database I am trying to persist a Booking object
I am trying to store an element with a many to one relationship with an element that already has a many to one relationship with another
Booking class has a many to one relationship with the Event class but the Event Class already has a many to one relationship with the Users Class Which I am also trying to save again within the same table
Booking{
//many to one
Users(owner)
//many to one
Users(user)
//many to one
Event(venue)
}
Event{
//many to one
Users(owner)
}
Users{
}
Booking.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 23, 2019 11:11:55 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="" table="booking" catalog="eventer">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity"></generator>
</id>
<property name="capacity" type="string">
<column name="capacity" not-null="true" />
</property>
<property name="date" type="string">
<column name="date" not-null="true" />
</property>
<property name="eventname" type="string">
<column name="eventname" not-null="true" />
</property>
<many-to-one name="venue" class="entities.Event" cascade="all">
<meta attribute="class-description" inherit="false">class containing product details</meta>
<column name="venue" />
</many-to-one>
<many-to-one name="owner" class="entities.Users" cascade="all">
<meta attribute="class-description" inherit="false">class containing product details</meta>
<column name="owner" />
</many-to-one>
<property name="pricing" type="int">
<column name="pricing" not-null="true" />
</property>
<many-to-one name="user" class="entities.Users" cascade="all">
<meta attribute="class-description" inherit="false">class containing product details</meta>
<column name="users" />
</many-to-one>
</class>
</hibernate-mapping>
Booking.java
package entities;
// Generated Jul 23, 2019 11:11:55 AM by Hibernate Tools 4.3.1
/**
* Booking generated by hbm2java
*/
public class Booking implements java.io.Serializable {
private Integer id;
private String capacity;
private String date;
private String eventname;
private Event venue;
private Users owner;
private int pricing;
private Users user;
public Booking() {
}
public Booking(String capacity, String date, String eventname, int pricing) {
this.capacity = capacity;
this.date = date;
this.eventname = eventname;
this.pricing = pricing;
}
public Booking(String capacity, String date, String eventname, Event venue, Users owner, int pricing, Users user) {
this.capacity = capacity;
this.date = date;
this.eventname = eventname;
this.venue = venue;
this.owner = owner;
this.pricing = pricing;
this.user = user;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCapacity() {
return this.capacity;
}
public void setCapacity(String capacity) {
this.capacity = capacity;
}
public String getDate() {
return this.date;
}
public void setDate(String date) {
this.date = date;
}
public String getEventname() {
return this.eventname;
}
public void setEventname(String eventname) {
this.eventname = eventname;
}
public Event getVenue() {
return this.venue;
}
public void setVenue(Event venue) {
this.venue = venue;
}
public Users getOwner() {
return this.owner;
}
public void setOwner(Users owner) {
this.owner = owner;
}
public int getPricing() {
return this.pricing;
}
public void setPricing(int pricing) {
this.pricing = pricing;
}
public Users getUser() {
return this.user;
}
public void setUser(Users user) {
this.user = user;
}
}
Event.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 23, 2019 11:11:55 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="entities.Event" table="event" catalog="eventer">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity"></generator>
</id>
<property name="image" type="string">
<column name="image" length="10000" not-null="true" />
</property>
<property name="location" type="string">
<column name="location" not-null="true" />
</property>
<property name="upto100" type="int">
<column name="upto100" not-null="true" />
</property>
<property name="upto300" type="java.lang.Integer">
<column name="upto300" />
</property>
<property name="upto500" type="java.lang.Integer">
<column name="upto500" />
</property>
<property name="upto1000" type="java.lang.Integer">
<column name="upto1000" />
</property>
<property name="phoneno" type="int">
<column name="phoneno" not-null="true" />
</property>
<property name="scenario" type="string">
<column name="scenario" not-null="true" />
</property>
<property name="name" type="string">
<column name="name" not-null="true" />
</property>
<property name="mail" type="string">
<column name="mail" not-null="true" />
</property>
<many-to-one name="owner" class="entities.Users" cascade="all">
<meta attribute="class-description" inherit="false">class containing product details</meta>
<column name="owner" />
</many-to-one>
<property name="description" type="string">
<column name="description" />
</property>
</class>
</hibernate-mapping>
Event.java
package entities;
// Generated Jul 23, 2019 11:11:55 AM by Hibernate Tools 4.3.1
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
/**
* Event generated by hbm2java
*/
public class Event implements java.io.Serializable {
private Integer id;
private String image;
private String location;
private int upto100;
private Integer upto300;
private Integer upto500;
private Integer upto1000;
private int phoneno;
private String scenario;
private String name;
private String mail;
private Users owner;
private String description;
public Event() {
}
public Event(String image, String location, int upto100, int phoneno, String scenario, String name, String mail) {
this.image = image;
this.location = location;
this.upto100 = upto100;
this.phoneno = phoneno;
this.scenario = scenario;
this.name = name;
this.mail = mail;
}
public Event(String image, String location, int upto100, Integer upto300, Integer upto500, Integer upto1000, int phoneno, String scenario, String name, String mail, Users owner, String description) {
this.image = image;
this.location = location;
this.upto100 = upto100;
this.upto300 = upto300;
this.upto500 = upto500;
this.upto1000 = upto1000;
this.phoneno = phoneno;
this.scenario = scenario;
this.name = name;
this.mail = mail;
this.owner = owner;
this.description = description;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getImage() {
return this.image;
}
public void setImage(String image) {
this.image = image;
}
public String getLocation() {
return this.location;
}
public void setLocation(String location) {
this.location = location;
}
public int getUpto100() {
return this.upto100;
}
public void setUpto100(int upto100) {
this.upto100 = upto100;
}
public Integer getUpto300() {
return this.upto300;
}
public void setUpto300(Integer upto300) {
this.upto300 = upto300;
}
public Integer getUpto500() {
return this.upto500;
}
public void setUpto500(Integer upto500) {
this.upto500 = upto500;
}
public Integer getUpto1000() {
return this.upto1000;
}
public void setUpto1000(Integer upto1000) {
this.upto1000 = upto1000;
}
public int getPhoneno() {
return this.phoneno;
}
public void setPhoneno(int phoneno) {
this.phoneno = phoneno;
}
public String getScenario() {
return this.scenario;
}
public void setScenario(String scenario) {
this.scenario = scenario;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getMail() {
return this.mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public Users getOwner() {
return this.owner;
}
public void setOwner(Users owner) {
this.owner = owner;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
/*public String toStreng(Event event) throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(event);
}
public Event fromStreng(String string) throws IOException {
return new ObjectMapper().readValue(string, Event.class);
}*/
}
Users.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 23, 2019 11:11:55 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="entities.Users" table="users" catalog="eventer">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity"></generator>
</id>
<property name="image" type="string">
<column name="image" length="1000" />
</property>
<property name="email" type="string">
<column name="email" not-null="true" unique="true" />
</property>
<property name="username" type="string">
<column name="username" not-null="true" unique="true" />
</property>
<property name="password" type="string">
<column name="password" not-null="true" />
</property>
<property name="firstname" type="string">
<column name="firstname" not-null="true" />
</property>
<property name="lastname" type="string">
<column name="lastname" not-null="true" />
</property>
<property name="phoneno" type="int">
<column name="phoneno" not-null="true" unique="true" />
</property>
<property name="gender" type="string">
<column name="gender" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>
Users.java
package entities;
// Generated Jul 23, 2019 11:11:55 AM by Hibernate Tools 4.3.1
/**
* Users generated by hbm2java
*/
public class Users implements java.io.Serializable {
private Integer id;
private String image;
private String email;
private String username;
private String password;
private String firstname;
private String lastname;
private int phoneno;
private String gender;
public Users() {
}
public Users(String email, String username, String password, String firstname, String lastname, int phoneno, String gender) {
this.email = email;
this.username = username;
this.password = password;
this.firstname = firstname;
this.lastname = lastname;
this.phoneno = phoneno;
this.gender = gender;
}
public Users(String image, String email, String username, String password, String firstname, String lastname, int phoneno, String gender) {
this.image = image;
this.email = email;
this.username = username;
this.password = password;
this.firstname = firstname;
this.lastname = lastname;
this.phoneno = phoneno;
this.gender = gender;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getImage() {
return this.image;
}
public void setImage(String image) {
this.image = image;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstname() {
return this.firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return this.lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getPhoneno() {
return this.phoneno;
}
public void setPhoneno(int phoneno) {
this.phoneno = phoneno;
}
public String getGender() {
return this.gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
来源:https://stackoverflow.com/questions/57165768/detached-entity-passed-to-persist-entities-event