版本:hibernate4.3.5
单向一对多,一端为主控端。一个person实体有多个address实体.采用主外键的方式。
person表有person_id,name,age三个cloumn,其中为PK。
address表有address_id,address_name,person_id,其中address_id为PK,person_id为FK,且为notNull。
person实体的定义:
address实体定义:
在client端,new两个address,再加到person的set中,最后persist这个person:
按书上的说法,这种情况下,如果address表的外键person_id为notnull,则直接出错;如果address表的外键person_id可以为null,则先insert,再update。
但是我现在的情况是address表的外键person_id为notnull,但是却没有出错,而是先insert后update。而且insert时,person_id直接就可以取到personId,接着再用相同的值再update一次。如下:
在oracle11g上运行会出这种情况,实验环境:oracle11g, jdk1.7,ojdbc6.jar
但在mysql上却按预期般的出错,实验环境:mysql5.6,jdk7
单向一对多,一端为主控端。一个person实体有多个address实体.采用主外键的方式。
person表有person_id,name,age三个cloumn,其中为PK。
address表有address_id,address_name,person_id,其中address_id为PK,person_id为FK,且为notNull。
person实体的定义:
@Id
@Column(name="PERSON_ID")
private int personId = 0;
@Column(name="NAME")
private String name = null;
@Column(name="AGE")
private int age = 0;
@OneToMany(targetEntity=Address_OneToMany.class)
@JoinColumn(name="PERSON_ID",nullable=false, referencedColumnName="PERSON_ID")
@Cascade(CascadeType.ALL)
private Set<Address_OneToMany> address = new HashSet<Address_OneToMany>();
省略setter,getter
address实体定义:
@Id
@Column(name="ADDRESS_ID")
private int addressId=0;
@Column(name="ADDRESS_NAME")
private String addressName = null;
省略setter,getter
在client端,new两个address,再加到person的set中,最后persist这个person:
Address address1 = new Address(411,"lv shun");
Address address2 = new Address(101,"beijing");
Person person1 = new Person(210824,"father",59);
person1.getAddress().add(address1);
person1.getAddress().add(address2);
session.persist(person1);
按书上的说法,这种情况下,如果address表的外键person_id为notnull,则直接出错;如果address表的外键person_id可以为null,则先insert,再update。
但是我现在的情况是address表的外键person_id为notnull,但是却没有出错,而是先insert后update。而且insert时,person_id直接就可以取到personId,接着再用相同的值再update一次。如下:
insert
into
TEST_PERSON
(AGE, NAME, PERSON_ID)
values
(?, ?, ?)
2014-12-19 14:18:29,378 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [1] as [INTEGER] - [59]
2014-12-19 14:18:29,379 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [2] as [VARCHAR] - [father]
2014-12-19 14:18:29,379 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [3] as [INTEGER] - [210824]
2014-12-19 14:18:29,478 DEBUG (org.hibernate.SQL:109) -
insert
into
TEST_ADDRESS
(ADDRESS_NAME, PERSON_ID, ADDRESS_ID)
values
(?, ?, ?)
2014-12-19 14:18:29,478 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [1] as [VARCHAR] - [lv shun]
2014-12-19 14:18:29,478 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [2] as [INTEGER] - [210824]
2014-12-19 14:18:29,479 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [3] as [INTEGER] - [411]
2014-12-19 14:18:29,575 DEBUG (org.hibernate.SQL:109) -
insert
into
TEST_ADDRESS
(ADDRESS_NAME, PERSON_ID, ADDRESS_ID)
values
(?, ?, ?)
2014-12-19 14:18:29,575 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [1] as [VARCHAR] - [beijing]
2014-12-19 14:18:29,575 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [2] as [INTEGER] - [210824]
2014-12-19 14:18:29,576 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [3] as [INTEGER] - [101]
2014-12-19 14:18:29,672 DEBUG (org.hibernate.SQL:109) -
update
TEST_ADDRESS
set
PERSON_ID=?
where
ADDRESS_ID=?
2014-12-19 14:18:29,672 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [1] as [INTEGER] - [210824]
2014-12-19 14:18:29,673 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [2] as [INTEGER] - [411]
2014-12-19 14:18:29,769 DEBUG (org.hibernate.SQL:109) -
update
TEST_ADDRESS
set
PERSON_ID=?
where
ADDRESS_ID=?
2014-12-19 14:18:29,769 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [1] as [INTEGER] - [210824]
2014-12-19 14:18:29,769 TRACE (org.hibernate.type.descriptor.sql.BasicBinder:81) - binding parameter [2] as [INTEGER] - [101]
在oracle11g上运行会出这种情况,实验环境:oracle11g, jdk1.7,ojdbc6.jar
但在mysql上却按预期般的出错,实验环境:mysql5.6,jdk7