Hibernate 注解单向一对多配置
2016-08-30 07:54
309 查看
***
* Plate表
* @author Swing
*
*/
@Entity
@Table(name = "tb_Plate")
public class Plate implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String PlateName;
private Date addTime;
private String addName;
private String Jurisdiction;
private String typeName;
private List<SubPlate> subPlate;
@OneToMany(fetch = FetchType.EAGER,targetEntity = SubPlate.class,cascade = //单项一对多配置
{
CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.MERGE,
})
@JoinColumns(value={@JoinColumn(name="Pid",referencedColumnName="id")}) //对应关系 Pid = id
public List<SubPlate> getSubPlate() {
return subPlate;
}
public void setSubPlate(List<SubPlate> subPlate) {
this.subPlate = subPlate;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPlateName() {
return PlateName;
}
public void setPlateName(String plateName) {
PlateName = plateName;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
public String getAddName() {
return addName;
}
public void setAddName(String addName) {
this.addName = addName;
}
public String getJurisdiction() {
return Jurisdiction;
}
public void setJurisdiction(String jurisdiction) {
Jurisdiction = jurisdiction;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
}
/***
* SubPlate表
* @author Swing
*
*/
@Entity
@Table(name = "tb_SubPlate")
public class SubPlate {
private static final long serialVersionUID = 1L;
private int id;
private String SubPlateName;
private Date addTime;
private String addName;
private int Pid;
private String Jurisdiction;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSubPlateName() {
return SubPlateName;
}
public void setSubPlateName(String subPlateName) {
SubPlateName = subPlateName;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
public String getAddName() {
return addName;
}
public void setAddName(String addName) {
this.addName = addName;
}
public int getPid() {
return Pid;
}
public void setPid(int pid) {
Pid = pid;
}
public String getJurisdiction() {
return Jurisdiction;
}
public void setJurisdiction(String jurisdiction) {
Jurisdiction = jurisdiction;
}
}
测试方法 HQL语句:
public class text {
public static void main(String[] args) {
Service service = new ServiceImpl();
List list = service.listQuery("select p from Plate p left join fetch p.subPlate e");
for(Plate p:(List<Plate>) list){
System.out.println("Plate:"+p.getPlateName());
for(SubPlate e:p.getSubPlate()){
System.out.println("Sub"+e.getSubPlateName());
}
}
}
}
* Plate表
* @author Swing
*
*/
@Entity
@Table(name = "tb_Plate")
public class Plate implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String PlateName;
private Date addTime;
private String addName;
private String Jurisdiction;
private String typeName;
private List<SubPlate> subPlate;
@OneToMany(fetch = FetchType.EAGER,targetEntity = SubPlate.class,cascade = //单项一对多配置
{
CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.MERGE,
})
@JoinColumns(value={@JoinColumn(name="Pid",referencedColumnName="id")}) //对应关系 Pid = id
public List<SubPlate> getSubPlate() {
return subPlate;
}
public void setSubPlate(List<SubPlate> subPlate) {
this.subPlate = subPlate;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPlateName() {
return PlateName;
}
public void setPlateName(String plateName) {
PlateName = plateName;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
public String getAddName() {
return addName;
}
public void setAddName(String addName) {
this.addName = addName;
}
public String getJurisdiction() {
return Jurisdiction;
}
public void setJurisdiction(String jurisdiction) {
Jurisdiction = jurisdiction;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
}
/***
* SubPlate表
* @author Swing
*
*/
@Entity
@Table(name = "tb_SubPlate")
public class SubPlate {
private static final long serialVersionUID = 1L;
private int id;
private String SubPlateName;
private Date addTime;
private String addName;
private int Pid;
private String Jurisdiction;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSubPlateName() {
return SubPlateName;
}
public void setSubPlateName(String subPlateName) {
SubPlateName = subPlateName;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
public String getAddName() {
return addName;
}
public void setAddName(String addName) {
this.addName = addName;
}
public int getPid() {
return Pid;
}
public void setPid(int pid) {
Pid = pid;
}
public String getJurisdiction() {
return Jurisdiction;
}
public void setJurisdiction(String jurisdiction) {
Jurisdiction = jurisdiction;
}
}
测试方法 HQL语句:
public class text {
public static void main(String[] args) {
Service service = new ServiceImpl();
List list = service.listQuery("select p from Plate p left join fetch p.subPlate e");
for(Plate p:(List<Plate>) list){
System.out.println("Plate:"+p.getPlateName());
for(SubPlate e:p.getSubPlate()){
System.out.println("Sub"+e.getSubPlateName());
}
}
}
}
相关文章推荐
- 使用Hibernate注解来配置多对一单向的三种方法
- Hibernate 注解单向一对多配置
- 使用Hibernate注解来配置多对一单向的三种方法
- Hibernate中用注解配置一对多双向关联和多对一单向关联
- Hibernate中用注解配置一对多双向关联和多对一单向关联
- Hibernate中用注解配置一对多双向关联和多对一单向关联
- hibernate单向xml映射配置对比
- Hibernate注解配置 在Spring的配置文件里写法
- Hibernate关联关系映射-----单向一对多配置
- Hibernate关联关系映射-----单向一对多配置
- Hibernate关联关系映射-----单向多对一映射配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- Hibernate 继承的注解配置
- spring+hibernate xml和注解方式配置事务
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- Hibernate关联关系映射-----单向一对一映射配置
- [注解的力量]Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- Hibernate关联关系映射-----基于连接表的单向一对多映射配置