The components of java hibernate mapping

1. In a development, and some type of information is more complex, and certain information may be composed of one part, the mapping assembly can be used at this time, the component is a mapping table mapping a plurality of classes. Table Structure

2. Class Design

Link.java

public class Link {
    private String phone;
    private String qq;
    private String email;
    private String address;
    public Link() {
    }
    
    public Link(String phone, String qq, String email, String address) {
        super();
        this.phone = phone;
        this.qq = qq;
        this.email = email;
        this.address = address;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getQq() {
        return qq;
    }
    public void setQq(String qq) {
        this.qq = qq;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

Person.java

public class Person {
    private int id;
    private String name;
    private int age;
    private Link link;
    public Person() {
    }
    
    public Person(String name, int age, Link link) {
        super();
        this.name = name;
        this.age = age;
        this.link = link;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Link getLink() {
        return link;
    }
    public void setLink(Link link) {
        this.link = link;
    }
    
}

3. mapping file information

<hibernate-mapping package="cn.sxt.pojo">
    <class name="Person" table="t_person_info">
        <id name="id">
            <generator class="native">
            </generator>
        </id>
        <property name="name"/>
        <property name="age"/>
        <!-- 组件映射 -->
        <component name="link" class="Link">
            <property name="phone"/>
            <property name="qq"/>
            <property name="email"/>
            <property name="address"/>
        </component>
    </class>
</hibernate-mapping>

4. Test

public  class HibernateTest {
     / ** 
     * means a method for generating database tables 
     * * / 
    @Test 
    public  void testCreateDB () { 
        the Configuration CFG = new new the Configuration () Configure ();. 
        the SchemaExport SE = new new the SchemaExport (CFG);
         // first sql script print parameters whether
         // if the second parameter to the database exported script execution 
        se.create ( to true , to true ); 
    } 
    / ** 
     * initialization table data 
     * / 
    @Test 
    public  void  testInit () {
        the Session the session =null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            
            Link l1 = new Link("1324115534","345324","[email protected]","多伦多");
            Link l2 = new Link("1345432367","675754","[email protected]","纽约");
            Person p1 = new Person("mike",23,l1);
            Person p2 = new Person("lily",40,l2);
            session.save(p1);
            session.save(p2);
            tx.commit();
            
        } catch (Exception e) {
            if(tx!=null)
                tx.rollback();
        }finally {
            HibernateUtil.close();
        }
    }
    /**
     */
    @Test
    public void testGetData(){
        Session session = HibernateUtil.getSession();
        Person p1 = (Person)session.get(Person.class, 1);
        System.out.println(p1);
        HibernateUtil.close();
    }
}

 

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11204769.html