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 组建映射 (两个表 合并成一张表)的主要内容,如果未能解决你的问题,请参考以下文章

在 Hibernate 中映射两个表 0..n

Hibernate多对多关系映射(建表)

Hibernate 一个类映射到多个表

如何在同一个数据库表上映射两个 JPA 或 Hibernate 实体

Hibernate映射文件如何配置触发器

Jpa Join 查询与来自两个表的数据,org.hibernate.MappingException:没有 JDBC 类型的方言映射:2002