日期:2014-05-16  浏览次数:20518 次

hibernate中list的映射

今天在看hibernate的帮助文档时,发现一对多,多对多,多对一关系映射中,原来list集合也是可以用的,以前只是在用set,必须要list的时候还进行转化。

?

hibernate list集合使用 User-Address

?

User类:

?

public class User {

?private String id;
?
?private String name;
?
?private String password;
?
?private List addressSet;

?

?......

?

}

?

Address类:

?

public class Address {

?private String id;
?
?private String address;
?
?private String city;

?

?.....

?

?

?

User.hbm.xml

?

<?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>
??? <class name="com.test.bean.User" table="t_user">

??????? <id name="id" type="java.lang.String">
??????? ?<column name="id" length="128" />
??????????? <generator class="uuid"></generator>
??????? </id>
??
??<property name="name" type="java.lang.String">
???<column name="tname"></column>
??</property>

??<property name="password" type="java.lang.String">
???<column name="tpassword"></column>
??</property>
??<!--? order-by="city"
??<set name="addressSet" table="t_add" cascade="all" lazy="false" inverse="false">
???<key column="userid"></key>
???<one-to-many class="com.test.bean.Address" />
??</set>
??-->
??
??<list name="addressSet" table="t_add" cascade="all" lazy="false" inverse="false">
???<key column="userid"></key>
???<list-index column="addindex" />
???<one-to-many class="com.test.bean.Address" />
??</list>
??
??? </class>

</hibernate-mapping>

?

Address.hbm.xml

?

<?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>
??? <class name="com.test.bean.Address" table="t_add">
??????? <id name="id" type="java.lang.String">
??????? ?<column name="id" length="128" />
??????????? <generator class="uuid"></generator>
??????? </id>
??
??<property name="address" type="java.lang.String">
???<column name="address"></column>
??</property>

??<property name="city" type="java.lang.String">
???<column name="city"></column>
??</property>

??? </class>

</hibernate-mapping>

?

?

?这样就可以正常使用了呢...但是不知道这样做有什么弊端不..?

?

?