简体   繁体   English

如何使用Hibernate映射组合键,其中一个是外键

[英]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.

相关问题 hibernate composite主键包含一个复合外键,如何映射这个 - hibernate composite Primary key contains a composite foreign key, how to map this 如何映射具有复合键的表,其中一列是 Hibernate 上的外键 - How to map a table with composite key with one of the column being foreign key on Hibernate 如何使用自动生成的值制作复合主键,并使用休眠和Spring MVC制作一个外键 - How make a composite primary key using auto generated value and one foreign key using hibernate and spring MVC 使用外键作为组合键的一部分进行休眠 - Hibernate using a foreign key as part of a composite key JPA,Hibernate,Java。 复合主键,其中之一也是外键 - JPA, Hibernate, Java. Composite primary key, one of them is also foreign key 使用Java Hibernate将复合主键映射到外键实体 - Map composite primary key to foreign key entities with Java Hibernate 休眠:如何在xml中创建复合外键? - hibernate: how to create a composite foreign key in xml? 如何使用JPA,Hibernate,Springboot实现复合主键和复合外键 - How to implement Composite Primary key and Composite Foreign Key using JPA,Hibernate, Springboot 如何用 hibernate 将 map 用作外键? - How to map a foreign key with hibernate? 如何使用休眠映射这些实体? 需要映射一对多关系。 外键未更新 - How can I map these entities using hibernate? Need to map one to many relationship. Foreign key is not updating
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM