单向List/Array映射:
Bean:
public class Car implements Serializable{ private int id ; private String name ; .... } public class User implements Serializable { private int id ; private String name ; private List<Car> cars ; .... }
xml:
Car.hbm.xml: <class name = "Car"> <!-- id是来配置主键 name是类中的哪个属性和主键映射 generator:表示主键的生成策略 --> <id name = "id"> <generator class="native" /> </id> <!-- property是用来把类中的属性和表的字段做一一对应 --> <property name="name" /> </class> User.hbm.xml: <class name = "User"> <!-- id是来配置主键 name是类中的哪个属性和主键映射 generator:表示主键的生成策略 --> <id name = "id"> <generator class="native" /> </id> <!-- property是用来把类中的属性和表的字段做一一对应 --> <property name="name" /> <list name="cars"> <key column="uid" /> <list-index column="carindex" /> <one-to-many class="Car"/> </list> </class>
单向Map映射:
Bean:
public class Car implements Serializable{ private int id ; private String name ; .... } public class User implements Serializable { private int id ; private String name ; private Map<String,Car> cars ; .... }
xml:
Car.hbm.xml: <class name = "Car"> <!-- id是来配置主键 name是类中的哪个属性和主键映射 generator:表示主键的生成策略 --> <id name = "id"> <generator class="native" /> </id> <!-- property是用来把类中的属性和表的字段做一一对应 --> <property name="name" /> </class> User.hbm.xml: <class name = "User"> <!-- id是来配置主键 name是类中的哪个属性和主键映射 generator:表示主键的生成策略 --> <id name = "id"> <generator class="native" /> </id> <!-- property是用来把类中的属性和表的字段做一一对应 --> <property name="name" /> <map name="cars"> <key column="uid" /> <!-- <map-key记录键 --> <map-key column="mk" type = "string"/> <one-to-many class="Car"/> </map> </class>