程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> hibernate----component-entity (人-地址-學校),entitycomponent

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

編輯:JAVA綜合教程

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;

@Embeddable
public 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();
    }
    }

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved