Hibernate中的一对一关系插入顺序

时间:2014-06-28 19:45:04

标签: hibernate flush one-to-one

我有两个实体,彼此之间具有一对一的关联。 两个实体都具有复合自然主键。一个实体使用其自然复合键中的1个字段和一个额外列来引用另一个实体。 我有持久化这些实体的问题,Hibernate首先插入引用对象(根据我的理解它是HallPlacesSchema)所以我在插入引用的对象之前立即得到约束违规错误(Hall)。

CREATE TABLE webgatemarket_TssHallPlacesSchema (
  TSSHALLPLACESSCHEMAID            INTEGER          NOT NULL,
  WEBGATEMARKET_ID                 INT(11) UNSIGNED NOT NULL,
  TSSHALLID                        INTEGER          NOT NULL,

  PRIMARY KEY (TSSHALLPLACESSCHEMAID, WEBGATEMARKET_ID),
  FOREIGN KEY (WEBGATEMARKET_ID, TSSHALLID) REFERENCES webgatemarket_TssHall(WEBGATEMARKET_ID, TSSHALLID),
  FOREIGN KEY (WEBGATEMARKET_ID) REFERENCES webgatemarkets (cinema_id),
  INDEX (WEBGATEMARKET_ID, TSSHALLPLACESSCHEMAID)
) ENGINE =InnoDB, DEFAULT CHARSET =utf8;

CREATE TABLE webgatemarket_TssHall (
  TSSHALLID           INTEGER          NOT NULL,
  WEBGATEMARKET_ID    INT(11) UNSIGNED NOT NULL,

  PRIMARY KEY (TSSHALLID, WEBGATEMARKET_ID),
  FOREIGN KEY (WEBGATEMARKET_ID) REFERENCES webgatemarkets (cinema_id)
) ENGINE =InnoDB, DEFAULT CHARSET =utf8;


public class DataObjectPk implements Serializable
{
    @Id
    private int id;

    @ManyToOne
    @JoinColumn(name = "WEBGATEMARKET_ID")
    private WebGateMarketDO webGateMarket;
}

@IdClass(DataObjectPk.class)
public class Hall
{
    @Id
    @Column(name = "TSSHALLID")
    private int id;

    @ManyToOne
    @JoinColumn(name = "WEBGATEMARKET_ID")
    private WebGateMarketDO webGateMarket;

    @OneToOne(mappedBy = "hall")
    private HallPlacesSchema hallPlacesSchema;
}

@IdClass(DataObjectPk.class)
public class HallPlacesSchema extends DataObject
{
    @Id
    private int id;

    @ManyToOne
    @JoinColumn(name = "WEBGATEMARKET_ID")
    private WebGateMarketDO webGateMarket;

    @Column(name = "TSSHALLID")
    private Integer hallId;

    @OneToOne
    @JoinColumns({
            @JoinColumn(name = "TSSHALLID", referencedColumnName = "TSSHALLID", insertable = false, updatable = false),
            @JoinColumn(name = "WEBGATEMARKET_ID", referencedColumnName = "WEBGATEMARKET_ID", insertable = false, updatable = false)
    })
    private Hall hall;
}

在单个会话中出现问题时,我会依次保持Hall(s)和HallPlacesSchema(s)。

entityManager.persist(halls); 
entityManager.persist(hallPlacesSchemas);
entityManager.flush();

当会话刷新时,我看到Hibernate首先INSERT了HallPlacesSchema对象,所以我立即得到约束违规错误:无法添加或更新子行:外键约束失败....

任何人都可以帮我理解为什么会这样吗?

Hibernate: 
    /* insert HallPlacesSchema
        */ insert 
        into
            webgatemarket_TssHallPlacesSchema
            (DELFLAG, UPDATENUM, TSSHALLPLACESCHEMA_BACKGROUND, TSSHALLID, TSSHALLPLACESCHEMA_HEIGHT, TSSHALLPLACESCHEMA_ISDEFAULT, TSSHALLPLACESCHEMA_NAME, TSSHALLPLACESCHEMA_PLACE_HEIGHT, TSSHALLPLACESCHEMA_PLACE_WIDTH, SAREAID, TSSHALLPLACESCHEMA_SCALE_FACTOR, TSSHALLPLACESCHEMA_SECTORS_COUNT, TSSHALLPLACESCHEMA_WIDTH, TSSHALLPLACESSCHEMAID, WEBGATEMARKET_ID) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2014-06-28 22:31:39,126 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] : SQL Error: 1452, SQLState: 23000
2014-06-28 22:31:39,126 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] : Cannot add or update a child row: a foreign key constraint fails.....

1 个答案:

答案 0 :(得分:0)

请忽略我的问题,我的测试类中有一个错误导致首先插入HallPlacesSchema。