Hibernate的关联映射包括:
一对一(Persion - IDCard)
一对多(Department - Employee)
多对一(Employee - Department)
多对多(Teacher - Student)
组件映射(User - Name)
集合映射(Set、List、Map)
inverse和cascade(Employee - Department)
本文以组件映射为例:
组件通常也是一个对象
扫描二维码关注公众号,回复:
845678 查看本文章
当这个对象要单独创建表的时候,就相当于一对一、一对多、多对一。
当这个对象不需要单独创建表的时候,想把自己的属性写到主对象表中的时候,就用到组件映射<component>
主对象User.hbm.xml配置文件如下:
<component name="name">
<property name="firstName" column="first_name"/>
<property name="lastName" column="last_name"/>
</component>
实体类:User.java 和组件类 Name.java
package com.cos.entity; public class User { private int id; private Name name; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Name getName() { return name; } public void setName(Name name) { this.name = name; } } ////////////////////////////////////////////////////////////////// package com.cos.entity; //此类不单独创建表,而是把字段写入到user表中 public class Name { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
映射文件:User.hbm.xml (注意:只有一个映射文件,没有组件类Name的映射文件)
<?xml version="1.0"?> <!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.cos.entity.User" lazy="true"> <id name="id"> <generator class="native"/> </id> <property name="age"/> <!-- 组件映射的时候,组件因为不需要自己创建一张表,所以把组件对象写到主对象表中 --> <component name="name"> <property name="firstName" column="first_name"/> <property name="lastName" column="last_name"/> </component> </class> </hibernate-mapping>
Hibernate配置文件:hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hi</property> <property name="hibernate.connection.username"></property> <property name="hibernate.connection.password"></property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">create</property> <property name="show_sql">true</property> <mapping resource="com/cos/entity/User.hbm.xml"/> </session-factory> </hibernate-configuration>
工具类:HibernateUtil.java
package com.cos.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public final class HibernateUtil { private static SessionFactory sesseionFactory; static { Configuration conf = new Configuration(); conf.configure(); sesseionFactory = conf.buildSessionFactory(); } public static SessionFactory getSesseionFactory() { return sesseionFactory; } }
测试类:Component.java
package com.cos.main; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.cos.entity.Name; import com.cos.entity.User; import com.cos.util.HibernateUtil; public class Component { public static void main(String[] args) { add(); } public static void add(){ SessionFactory sessionFactory = null; Session s = null; Transaction t = null; try{ Name name = new Name(); name.setFirstName("zhang"); name.setLastName("san"); User user = new User(); user.setAge(28); user.setName(name); sessionFactory = HibernateUtil.getSesseionFactory(); s = sessionFactory.openSession(); t = s.beginTransaction(); //组件映射的时候,组件因为不需要自己创建一张表,所以把组件对象写到主对象表中 s.save(user); t.commit(); s.close(); sessionFactory.close(); }catch(Exception e){ e.printStackTrace(); } } }
表结构:
CREATE TABLE `user` ( `id` int(11) NOT NULL auto_increment, `age` int(11) default NULL, `first_name` varchar(255) default NULL, `last_name` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1