关闭

Hibernate 1-1(注解实现,基于外键列的单向无连接表)

标签: hibernate
92人阅读 评论(0) 收藏 举报
分类:
package com.javaee.book.hibernate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@Entity
@Table(name = "person_inf")
public class Person {

    @Id
    @Column(name = "person_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    private int age;
    @OneToOne(targetEntity = Address.class)
@JoinColumn(name="address_id",referencedColumnName="address_id",unique=true)

    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.javaee.book.hibernate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "address_inf")
public class Address {
    @Id
    @Column(name="address_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int addressId;
    private String addressDetail;

    public Address() {
        // Hibernate调用
    }

    public Address(String detail) {
        this.addressDetail = detail;
    }

    public int getAddressId() {
        return addressId;
    }

    public void setAddressId(int addressId) {
        this.addressId = addressId;
    }

    public String getAddressDetail() {
        return addressDetail;
    }

    public void setAddressDetail(String addressDetail) {
        this.addressDetail = addressDetail;
    }

}
public class TestDemo {
    public static void main(String[] args) {
        testOneToOne1();
    }


    public static void testOneToOne1(){

        //加载hibernate.cfg.cml文件
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure().build();
        SessionFactory sessionFactory = new MetadataSources(registry)
                .buildMetadata().buildSessionFactory();
        // 获取Session
        Session session = sessionFactory.openSession();
        // 获取Transaction
        Transaction transaction = session.beginTransaction();

        Person person=new Person();
        person.setName("British Man");
        person.setAge(21);
        Address a1=new Address("NewBalance");
        person.setAddress(a1);
        // 先持久化Address对象(对应为插入主表记录)
        session.persist(a1);
        // 再持久化Person对象(对应为插入从表记录)
        session.save(person);

        transaction.commit();
        session.close();
        sessionFactory.close();

    }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:37488次
    • 积分:1863
    • 等级:
    • 排名:千里之外
    • 原创:154篇
    • 转载:13篇
    • 译文:5篇
    • 评论:3条
    最新评论