[英]How to map a composite key and one of them is a foreign key using Hibernate
I have a table who has a composite key(picture one) and one of them is a foreign key that refers another table (picture two) 我有一个表,其中有一个组合键(图一),其中一个是引用另一个表的外键(图二)
How can I map this using hibernate??? 我如何使用休眠映射此???
The code below is the code I made. 下面的代码是我编写的代码。 But its is not working.
但是它不起作用。 I think it is not the way to make this map.
我认为这不是制作这张地图的方法。
The class CamposObrigatoriosID is the class that have CamposObrigatoriosID类是具有以下内容的类
@Embeddable
public class CamposObrigatoriosID implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name=“PERFIL_CMPOBR”)
private Long perfil;
@Column(name=“ORDEM”)
private Long ordem;
@Column(name=“NOMETABELA”)
private String nomeTabela;public Long getPerfil() {
return perfil;
}
...
@Override
public boolean equals(Object arg0) {
if(arg0 instanceof CamposObrigatoriosID) {
CamposObrigatoriosID that = (CamposObrigatoriosID) arg0;
return this.perfil.equals(that.perfil) && this.ordem.equals(that.ordem) && this.nomeTabela.equals(that.nomeTabela);
}
return false;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return perfil.hashCode() + ordem.hashCode() + nomeTabela.hashCode();
}
@Embeddable
public class CamposObrigatoriosID implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name=“PERFIL_CMPOBR”)
private Long perfil;
@Column(name=“ORDEM”)
private Long ordem;
@Column(name=“NOMETABELA”)
private String nomeTabela;
.....
@Entity
@Table(name=“USUARIO_PERFIL”)
public class UsuarioPerfil {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name=“CODIGO_USRPERFIL”)
private Long id;
@Column(name=“DESCRICAO_USRPERFIL”)
private String desccricaoUsrPerfil;
...
Error: SQLState:42000, ISC error code:335544351
错误:
SQLState:42000, ISC error code:335544351
You should use a "derived identity". 您应该使用“派生身份”。 Although you do not include the code for
CamposObrigatorios
, I'm guessing it should look something like this: 尽管您不包括
CamposObrigatorios
的代码,但我猜它应该看起来像这样:
@Entity
@Table(name = "CAMPOS_OBRIGATORIOS")
public class CamposObrigatorios {
@EmbeddedId
private CamposObrigatoriosID id;
@ManyToOne
@JoinColumn(name = "PERFIL_CMPOBR", referencedColumnName = "CODIGO_USRPERFIL")
@MapsId("perfil") // maps 'perfil' attribute of embedded id
private UsuarioPerfil usuarioPerfil;
...
}
Derived identities are discussed (with examples) in the JPA 2.1 spec in section 2.4.1. JPA 2.1规范的第2.4.1节中讨论了派生身份(带有示例)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.