在hibernate中一对多映射ConstraintViolationException

时间:2015-02-12 08:48:28

标签: java 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 mapping

<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)。但是,在添加相应的节目时,theaterID被用作0.可以任何人指向我这个问题的根本原因?

2 个答案:

答案 0 :(得分:0)

在Show Pojo中,你已经将threaterId定义为int,但它应该是Threater pojo的一个对象。

在休眠状态下,如果你想使用O2m关系,你必须在pojo中定义相同的内容。

所以,为此你必须修改你的节目pojo为

public class Shows{
private int showId;
private String showType;

@OneToMany
@JoinColumn(name="theaterId")
private Threater theater;

//getters and setters
}

它对我有用。请尝试

答案 1 :(得分:0)

看起来我正在尝试进行双向关联,并且忘记在Shows类中为剧院添加多对一映射。此外,正如The N所建议的那样, 需要用剧院参考替换theaterID。

添加了以下映射

<many-to-one
            name="theater"
            cascade="all"
            column = "theater_id"
            class="hibernate.Theater"/>

还发现应该删除th.setTheaterId(4);,因为自动生成了TheaterId。