The java hibernate based on the two-way primary key association mapping

This bidirectional primary key association mapping

1. investigation and the identity card is still one relationship, if the associated primary key, then the table structure is:

2. Class structure

Person.java

public class Person implements Serializable{
    private String id;
    private String name;
    private IdCard idCard;
    public Person() {
    }
    public Person(String name) {
        super();
        this.name = name;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public IdCard getIdCard() {
        return idCard;
    }
    public void setIdCard(IdCard idCard) {
        this.idCard = idCard;
    }
}

IdCard.java

public class IdCard implements Serializable{
    private String id;
    private String address;
    private Person person;
    public IdCard() {
    }
    
    public IdCard(String id, String address) {
        super();
        this.id = id;
        this.address = address;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public Person getPerson() {
        return person;
    }
    public void setPerson(Person person) {
        this.person = person;
    }
}

3. mapping file

Person.hbm.xml

<-Hibernate Mapping Package = "cn.sxt.pojo"> 
    < class name = " the Person " Table = "t_person"> 
        <name ID = "ID"> 
            <-! a front master key table is a foreign key references to acts as -> 
            <Generator class = "Foreign"> 
                 <param name = "Property"> idCard </ param> 
            </ Generator> 
        </ ID> 
        <Property name = "name" /> 
        <-! single master key based on the association   represents a foreign key constraint constrained -> 
        <One-to-One name = "idCard" constrained = "to true" /> 
    </class>
</hibernate-mapping>

IdCard.hbm.xml

<hibernate-mapping package="cn.sxt.pojo">
    <class name="IdCard" table="t_idCard">
        <id name="id">
            <generator class="assigned"></generator>
        </id>
        <property name="address"/>
        <one-to-one name="person"></one-to-one>
    </class>
</hibernate-mapping>

4. Test

public  class HibernateTest {
     / ** 
     * generation of database tools method 
     * * / 
    @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(){
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            IdCard card1 = new IdCard("110086", "湖北武当");
            IdCard card2 = new IdCard("110087", "光明顶");
            Person p1 = new Person("张三疯");
            p1.setIdCard(card1);
            Person p2 = new Person("殷素素");
            p2.setIdCard(card2);
            session.save(card1);
            session.save(card2);
            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, "110086");
        System.out.println(p1.getName()+"----"+p1.getIdCard().getId()+"----"+p1.getIdCard().getAddress());
        System.out.println("==================================");
        IdCard card =(IdCard)session.get(IdCard.class, "110086");
        System.out.println(card.getId()+"----"+card.getAddress()+"----"+card.getPerson().getName());
        HibernateUtil.close();
    }
}

 

Guess you like

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