代码:
一方:
import java.util.List;
public class Father {
private Long id;
List<Son> sons;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Son> getSons() {
return sons;
}
public void setSons(List<Son> sons) {
this.sons = sons;
}
}
多方:
public class Son {
private Long id;
private String sort;
private Father father;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSort() {
return sort;
}
public void setSort(String sort) {
this.sort = sort;
}
public Father getFather() {
return father;
}
public void setFather(Father father) {
this.father = father;
}
}
配置文件:
一方:
不能同时配置 <order-by >和<order-column> 会报如下错误,
Invalid content was found starting with element 分开配置:
<?xml version="1.0" encoding="UTF-8"?> <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" version="2.0"> <package>...model...</package> <access>PROPERTY</access> <entity class="Father" name="Father"> <table name="tb_father" /> <sequence-generator name="SEQ_tb_father_ID" sequence-name="SEQ_tb_father_ID" allocation-size="1"/> <attributes> <id name="id"> <column name="ID" unique="true" nullable="false" precision="11" scale="0" /> <generated-value strategy="SEQUENCE" generator="SEQ_tb_father_ID" /> </id> <!--1.对应关系跟下面2一样 :查询子list时会加一个order by sort_order ASC的条件--> <one-to-many name="sons" fetch="LAZY" mapped-by="father"> <order-by >sort_order ASC</order-by> <cascade><cascade-all/></cascade> </one-to-many> <!--2.对应关系跟上面1一样,分开写两次为了避开上面红字的报错。 插入或更新时会记录顺序,通过在tb_son的sort_order中从0递增记录--> <one-to-many name="sons" fetch="LAZY" mapped-by="resource"> <order-column name="sort_order" insertable="true" updatable="true"/> </one-to-many> </attributes> </entity> </entity-mappings>
多方:
<?xml version="1.0" encoding="UTF-8"?> <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" version="2.0"> <package>...model...</package> <access>PROPERTY</access> <entity class="Son" name="Son"> <table name="tb_son" /> <sequence-generator name="SEQ_tb_son_ID" sequence-name="SEQ_tb_son_ID" allocation-size="1"/> <attributes> <id name="id"> <column name="ID" unique="true" nullable="false" precision="11" scale="0" /> <generated-value strategy="SEQUENCE" generator="SEQ_tb_son_ID" /> </id> <basic name="sort"> <column name="sort_order" length="64" nullable="true" /> </basic> <many-to-one name="father" fetch="LAZY"> <join-column name="father_ID" insertable="true" updatable="true" /> <cascade><cascade-persist/><cascade-merge/><cascade-refresh/></cascade> </many-to-one> </attributes> </entity> </entity-mappings>
表结构如下:
| tb_father | tb_son
| id | id
| | sort_order
| | father_ID
father里有两个son1、son2,save(father)时:
tb_father 表数据:
id
1
tb_son 表数据:
id sort_order father_id
1 0 1
2 1 1