java - When does Hibernate create a new entry from a FK to a non-PK column? -
suppose have:
-- table_1 ------------------- |id | property_x | data | ------------------------------ -- table_2 ------------------------ |id | property_x | moar_data | -----------------------------------
and table_1
has fk property_x
table_2.property_x
(why not table_2.id
? idk, project :( )
the hbms this:
<!-- table 1 --> <hibernate-mapping package="com.example"> <class name="table1" table="table_1"> <id column="id" type="integer" name="id"> <generator class="native"/> </id> <many-to-one class="table2" fetch="join" name="table2" not-null="false"> <column name="property_x" /> </many-to-one> </class> </hibernate-mapping>
and
<!-- table 2 --> <hibernate-mapping package="com.example"> <class name="table2" table="table_2"> <id column="id" type="integer" name="id"> <generator class="native"/> </id> <property column="property_x" name="propertyx" type="string"/> </class> </hibernate-mapping>
the thing want save object table1
using session.save(objtable1)
, without creating new table2 entry in db, , without loading either. have done in past creating new object, , setting primary key values, leaving else blank, , not letting update table2
table, not pk >_<.
now, suppose have in table_2
entry with id=5
, property_x="ex"
. when following...
// example method public void savesomething(sessions sess) { table1 table1 = new table1(); table2 table2 = new table2(); table2.setpropertyx("ex"); table1.settable2(table2); sess.save(table1); }
... creates new entry, i'm guessing it's because pk (id) table2
not set.
my question is, how hibernate decide create new entry in db fk object?, , there way avoid on hbm files?
it's cascade anotation, if dont want table 2 created in db, dont set child, if want table 2 updated, need table 2 id.
alternatively, can set cascade="none" in hbm of table 1 or cascade="update", table 2 still wont updated without id.
Comments
Post a Comment