hibernate 组建映射 (两个表 合并成一张表)
Posted 跳刀玛利亚
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate 组建映射 (两个表 合并成一张表)相关的知识,希望对你有一定的参考价值。
package com.bjsxt.hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.OneToOne;
public class Wife {
private String wifename;
private String age;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getWifename() {
return wifename;
}
public void setWifename(String wifename) {
this.wifename = wifename;
}
}
······················································
package com.bjsxt.hibernate;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
@Entity
public class Husband {
private int id;
private String name;
private Wife wife;
@Id
@GeneratedValue
public int getId() {
return id;
}
public String getName() {
return name;
}
//@JoinColumn(name="wife_id")
// @OneToOne
// @JoinColumns(
// {
// @JoinColumn(name="wifeId",referencedColumnName="id"),
// @JoinColumn(name="wifeName",referencedColumnName="name")
// }
//
// )
@Embedded //不能写在变量上
public Wife getWife() {
return wife;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
以上是关于hibernate 组建映射 (两个表 合并成一张表)的主要内容,如果未能解决你的问题,请参考以下文章
如何在同一个数据库表上映射两个 JPA 或 Hibernate 实体
Jpa Join 查询与来自两个表的数据,org.hibernate.MappingException:没有 JDBC 类型的方言映射:2002