码迷,mamicode.com
首页 > Web开发 > 详细

注解:【无连接表的】Hibernate单向N->1关联

时间:2016-05-03 20:02:35      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

Person与Address关联:单向N->1,【无连接表的】

Person.java

package org.crazyit.app.domain;

import javax.persistence.CascadeType;
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.ManyToOne;
import javax.persistence.Table;

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

@Column(name="person_id")
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

private String name;

private Integer age;

//单向N->1:多对一关联关系
@ManyToOne(
targetEntity=Address.class,//指定关联对象
cascade=CascadeType.ALL//指定级联方式
)
//指定外键列,指定外键列的列名为address_id,不允许为空
@JoinColumn(name="address_id",nullable=false)
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 Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

}

Address.java

package org.crazyit.app.domain;

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
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="address_id")
private Integer id;
//地址详细
private String addressDetail;
//无参构造方法
public Address() {
}
//初始化全部成员变量
public Address(String addressDetail) {
this.addressDetail = addressDetail;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAddressDetail() {
return addressDetail;
}
public void setAddressDetail(String addressDetail) {
this.addressDetail = addressDetail;
}

}

//数据表

技术分享

技术分享

注解:【无连接表的】Hibernate单向N->1关联

标签:

原文地址:http://www.cnblogs.com/xiaowei1763369680/p/5456156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!