hibernate 注解 一对一和一对多


package bdqn.entity.oneToone;

import javax.persistence.*;

/**
 * 一对一注解
 */
@Entity
@Table(name = "Card")
public class Card {
    @Id
    @GeneratedValue   //主键生成策略
    private Integer cid;
    @Column  //表中对应的列名
    private String cnum;
          //cascade 级联操作(删除,)
    @OneToOne(mappedBy = "card",cascade = CascadeType.ALL)//建立一对一的关系
    private Pepole people;

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCnum() {
        return cnum;
    }

    public void setCnum(String cnum) {
        this.cnum = cnum;
    }

    public Pepole getPeople() {
        return people;
    }

    public void setPeople(Pepole people) {
        this.people = people;
    }
}
package bdqn.entity.oneToone;



import javax.persistence.*;

/**
 * Created by 一对一 on 2017/10/9.
 */
@Entity
@Table(name="Pepole")
public class Pepole {
    @Id
    @GeneratedValue
    private Integer pid;
    @Column
    private String pname;
    @OneToOne
    @JoinColumn(name="cid")
    private Card card;
    public Integer getPid() {
        return pid;
    }
    public void setPid(Integer pid) {
        this.pid = pid;
    }
    public String getPname() {
        return pname;
    }
    public void setPname(String pname) {
        this.pname = pname;
    }
    public Card getCard() {
        return card;
    }
    public void setCard(Card card) {
        this.card = card;
    }
}
xml version='1.0' encoding='utf-8'?>
 hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriverproperty>
    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orclproperty>
    <property name="connection.username">scottproperty>
    <property name="connection.password">1234property>
    
    <property name="dialect">org.hibernate.dialect.Oracle10gDialectproperty>
        
    <property name="show_sql">trueproperty>
    
    <property name="hbm2ddl.auto">updateproperty>
        
        
     

session-factory >
hibernate-configuration >

测试:
 @Test
    public void test03() {
        Pepole people=new Pepole();
        Card card=new Card();
        people.setPname("123");
        card.setCnum("123133");
        card.setPeople(people);
        people.setCard(card);
        session.save(card);
}


一对多



 
  
package bdqn.entity.oneToMoney;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
/**
 * 一对多注解
 */
@Entity
@Table(name ="Depts" )
public class Dept {
    @Id
    @GeneratedValue
    private Integer did;
    @Column
    private String name;
    @OneToMany(mappedBy = "dept",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    private Set emp=new HashSet();
    public Integer getDid() {
        return did;
    }
    public void setDid(Integer did) {
        this.did = did;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Set getEmp() {
        return emp;
    }
    public void setEmp(Set emp) {
        this.emp = emp;
    }
}
package bdqn.entity.oneToMoney;

import javax.persistence.*;

/**
 * 多对一
 */
@Entity
@Table(name = "Emps")
public class Emp {
    @Id
    @GeneratedValue
    private Integer eid;
    @Column
    private String name;
    @ManyToOne
    @JoinColumn(name = "did")
    private Dept dept;

    public Integer getEid() {
        return eid;
    }

    public void setEid(Integer eid) {
        this.eid = eid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }
}
xml version='1.0' encoding='utf-8'?>
 hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriverproperty>
    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orclproperty>
    <property name="connection.username">scottproperty>
    <property name="connection.password">1234property>
    
    <property name="dialect">org.hibernate.dialect.Oracle10gDialectproperty>
        
    <property name="show_sql">trueproperty>
    
    <property name="hbm2ddl.auto">updateproperty>
        
        
        <mapping class="bdqn.entity.oneToMoney.Dept">mapping>
        <mapping class="bdqn.entity.oneToMoney.Emp">mapping>
    session-factory>
hibernate-configuration>
测试:
//一对多
@Test
public void test09() {
    Dept dept=new Dept();
    dept.setName("财务部");
    bdqn.entity.oneToMoney.Emp emp=new bdqn.entity.oneToMoney.Emp();
    emp.setName("aa");
    dept.getEmp().add(emp);
    emp.setDept(dept);
    session.save(dept);
}

你可能感兴趣的:(hibernate 注解 一对一和一对多)