[英]Many-to-Many mapping Hibernate getting ConstraintViolationException
[英]One to many mapping ConstraintViolationException in hibernate
我有兩個表,劇院和節目,它們由一對多的映射連接。我試圖按如下方式執行收集持久性。
public class Theater
{
private int theaterId;
private String theaterName;
private List<Shows> shows;
//getters and setters
}
public class Shows{
private int showId;
private String showType;
private int theaterId;
//getters and setters
}
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
Transaction t = s.beginTransaction();
Theater th = new Theater("iNox");
th.setTheaterId(4);
List<Shows> shs = new ArrayList<Shows>();
shs.add(new Shows("noon"));
shs.add(new Shows("Drama"));
th.setShows(shs);
s.save(th);
t.commit();
s.close();
xml映射
<class name="hibernate.Theater"
table="theater">
<id name="theaterId"
column ="theater_id">
<generator class="increment"></generator>
</id>
<list name="shows" table = "shows" cascade = "all" inverse = "false">
<key column="theater_id" />
<index column="show_id"/>
<one-to-many class = "hibernate.Shows"/>
</list>
<property name = "theaterName" column = "theater_name" />
</class>
<class name="hibernate.Shows"
table="shows">
<id name="showId"
column ="show_id">
<generator class="increment"></generator>
</id>
<property name = "showType" column = "show_type"/>
<property name = "theaterId" column = "theater_id"/>
</class>
在此處添加相關日志
12:19:43,435 DEBUG EntityPrinter:114 - Listing entities:
12:19:43,435 DEBUG EntityPrinter:121 - hibernate.Shows{showId=4, showType=Drama, theaterId=0}
12:19:43,435 DEBUG EntityPrinter:121 - hibernate.Theater{theaterName=iNox, theaterId=4, shows=[hibernate.Shows#3, hibernate.Shows#4]}
12:19:43,435 DEBUG EntityPrinter:121 - hibernate.Shows{showId=3, showType=noon, theaterId=0}
12:19:43,442 DEBUG SQL:109 - insert into sakila.theater (theater_name, theater_id) values (?, ?)
Hibernate: insert into sakila.theater (theater_name, theater_id) values (?, ?)
12:19:43,445 TRACE BasicBinder:81 - binding parameter [1] as [VARCHAR] - [iNox]
12:19:43,445 TRACE BasicBinder:81 - binding parameter [2] as [INTEGER] - [4]
12:19:43,446 DEBUG SQL:109 - insert into sakila.shows (show_type, theater_id, show_id) values (?, ?, ?)
Hibernate: insert into sakila.shows (show_type, theater_id, show_id) values (?, ?, ?)
12:19:43,447 TRACE BasicBinder:81 - binding parameter [1] as [VARCHAR] - [noon]
12:19:43,447 TRACE BasicBinder:81 - binding parameter [2] as [INTEGER] - [0]
12:19:43,447 TRACE BasicBinder:81 - binding parameter [3] as [INTEGER] - [3]
12:19:43,448 DEBUG SqlExceptionHelper:139 - could not execute statement [n/a]
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails
基於日志,我猜通過自動增量正確生成了TheaterID(從劇院表中引用的外鍵)(theaterID = 4)。但是,在添加相應的節目時,劇院ID被用作0.可以任何人指出我的根本原因這個問題?
在Show Pojo中,您已將threaterId定義為int,但它應該是Threater pojo的對象。
在休眠狀態下,如果要使用O2m關系,則必須在pojo中定義相同的關系。
所以,為此你必須修改你的節目pojo as
public class Shows{
private int showId;
private String showType;
@OneToMany
@JoinColumn(name="theaterId")
private Threater theater;
//getters and setters
}
這個對我有用。 請試試
看起來我正在嘗試進行雙向關聯,並且忘記了在劇集類中為劇院添加多對一映射。同樣,如N所示,需要用劇院參考替換theaterID。
添加了以下映射
<many-to-one
name="theater"
cascade="all"
column = "theater_id"
class="hibernate.Theater"/>
還發現了th.setTheaterId(4);
應該在劇院自動生成時刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.