Is it possible in Hibernate to describe following mapping:
+------+ +------+
|Table1| |Table2|
+------+ +------+
|t1_id | -------+ |t2_id |
+------+ 开发者_运维百科 +------- |t1_id |
|data_1|
|data_2|
+------+
with class, described by Table1
, having two Map
s: { t2_id : data_1 }
and { t2_id : data_2 }
, or I absolutely must divide it on two tables?
If you don't mind, please use .hbm.xml
style.
It requires only one map:
public class Table1 {
private int id;
private Map<String, Table2> map;
}
public class Table2 {
private String data_1;
private String data_2;
}
mapping file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field" package="test">
<class name="Table1" table="table1">
<id column="ID" name="id">
<generator class="native"/>
</id>
<map name="map" table="table2">
<key column="TABLE1_ID"/>
<map-key column="TABLE2_ID" length="32" type="string"/>
<composite-element class="Table2">
<property name="data_1" column="DATA_1" length="128"/>
<property name="data_2" column="DATA_1" length="128"/>
</composite-element>
</map>
</class>
</hibernate-mapping>
The schema generated by SchemaExport:
alter table table2
drop constraint FKCB773E242504FFD6;
drop table table1 if exists;
drop table table2 if exists;
create table table1 (
ID integer generated by default as identity,
primary key (ID)
);
create table table2 (
TABLE1_ID integer not null,
DATA_1 varchar(128),
TABLE2_ID varchar(32) not null,
primary key (TABLE1_ID, TABLE2_ID)
);
alter table table2
add constraint FKCB773E242504FFD6
foreign key (TABLE1_ID)
references table1;
精彩评论