hibernate----component-entity (人-地址-學校),entitycomponent

來源:互聯網
上載者:User

hibernate----component-entity (人-地址-學校),entitycomponent

package com.ij34.dao;import javax.persistence.*;@Entity@Table(name="school_inf")public class School {     @Id @Column(name="school_id")     @GeneratedValue(strategy=GenerationType.IDENTITY)    private Integer id;    private String name;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    }

 

 

 

package com.ij34.dao;import javax.persistence.*;@Entity@Table(name="people_inf")public class People implements java.io.Serializable{    private static final long serialVersionUID = 1L;    @Id @Column(name="people_id")    @GeneratedValue(strategy=GenerationType.IDENTITY)    private Integer id;    private String name;    private int age;    private Address address;    public Integer getId() {        return id;    }    public void setId(Integer 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 Address getAddress() {        return address;    }    public void setAddress(Address address) {        this.address = address;    }}

 

 

package com.ij34.dao;import java.util.HashSet;import java.util.Set;import javax.persistence.*;import org.hibernate.annotations.Parent;@Embeddablepublic class Address{    @Column(name="message")    private String message;    @Parent    private People people;    @OneToMany(targetEntity=School.class)    @JoinColumn(name="address_id",referencedColumnName="people_id")    private Set<School> school=new HashSet<>();    public String getMessage() {        return message;    }    public void setMessage(String message) {        this.message = message;    }    public People getPeople() {        return people;    }    public void setPeople(People people) {        this.people = people;    }    public Set<School> getSchool() {        return school;    }    public void setSchool(Set<School> school) {        this.school = school;    }    }
package com.ij34.web;    import org.hibernate.Session;    import org.hibernate.SessionFactory;    import org.hibernate.Transaction;    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;    import org.hibernate.cfg.Configuration;    import org.hibernate.service.*;import com.ij34.dao.Address;import com.ij34.dao.People;import com.ij34.dao.School;    public class test01 {    public static void main(String[] args)throws Exception {    //執行個體化Configuration    Configuration conf=new Configuration().configure();    ServiceRegistry SR=new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();    // 以Configuration執行個體建立SessionFactory執行個體    SessionFactory SF=conf.buildSessionFactory(SR);    //create session    Session session=SF.openSession();    //start 事務    Transaction tx=session.beginTransaction();    People person = new People();    person.setAge(29);    // 為複合主鍵的兩個成員設定值   People people=new People();   people.setAge(22);   people.setName("林彪");   // 持久化People對象(對應於插入主表記錄)   session.save(people);   Address a=new Address();   a.setMessage("廣州");   people.setAddress(a);   School s1=new School();   School s2=new School();   s1.setName("哈佛大學");   s2.setName("社會大學");   session.save(s1);   session.save(s2);   a.getSchool().add(s2);   a.getSchool().add(s1);    tx.commit();    session.close();    SF.close();    }    }

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.