当前位置: 首页 > 知识库问答 >
问题:

用JPA映射复合主键和外键

谭思博
2023-03-14

我有两个表:A和B,都有一个复合主键。表B的PK也是表a主键的外键。

当我试图获取映射表B的类的实例时,我得到了以下异常

org.hibernate.TypeMismatchException

@Entity @Table(name="usuarios")
public class UsuarioEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId @AttributeOverrides( {
        @AttributeOverride(name="perTipoUsu", column=@Column(name="per_tipo_usu", nullable=false) ), 
        @AttributeOverride(name="perCodUsu", column=@Column(name="per_cod_usu", nullable=false) ) } )
    private UsuarioPKEntity pk = null;

   @OneToOne @PrimaryKeyJoinColumn
   private PersonaRelEnity personaRel = null;

   private String nombre;

   ....
}
@Entity @Table(name="persona_rel")
public class PersonaRelEnity implements Serializable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private PersonaRelPKEntity id;

    private String persona;

   ....
}

这两个类都位于package:com.cairone.ejemple01.entities

完整的日志输出为:

2016-09-21 12:28:24.505错误8568--[main]
O.S.Boot.SpringApplication:应用程序启动失败

java.lang.IllegalStateException: Failed to execute CommandLineRunner
  at     org.springframework.boot.SpringApplication.callRunner(SpringApplication.ja

2016-09-21 12:28:24.508信息8568--[main]utoConfigurationReportLoggingInitializer:

启动ApplicationContext时出错。要显示自动配置报告,请启用调试日志记录(从--debug开始)

2016-09-21 12:28:24.509  INFO 8568 --- [           main]     s.c.a.AnnotationConfigApplicationContext : Closing    

org.springframework.context.annotation.annotationconfigapplicationcontext@24b1d79b:启动日期[Wed Sep 21 12:28:21 ART 2016];上下文层次结构的根2016-09-21 12:28:24.511 INFO 8568---[main]O.S.J.E.A.AnnotationMBeanExporter:关闭时注销JMX公开的bean 2016-09-21 12:28:24.513 INFO 8568---[main]J.LocalContainerEntityManagerFactoryBean:关闭持久性单元“default”的JPA
EntityManagerFactory

这是相关部分:

org.hibernate.TypeMismatchException:为类com.cairone.ejemploy01.entities.PersonareLenity提供了错误类型的id。应为:类com.cairone.ejcomple01.entities.personarelpkentity,已获得类com.cairone.ejcomple01.entities.usuariopkentity

共有1个答案

师建德
2023-03-14

这是一个“派生标识”,其中一个实体的主键是从另一个相关实体的主键派生的。

我没有遇到过和这种关系完全一样的事情,但试试这个:

@Entity
@Table(name="persona_rel")
public class PersonaRelEnity implements Serializable {

    @EmbeddedId
    private PersonaRelPKEntity pk;

    ...
}

@Embeddable
public class PersonaRelPKEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name="per_tipo")
    private Integer perTipo;

    @Column(name="per_cod")
    private Integer perCod;

    ...
}

@Entity
@Table(name="usuarios")
public class UsuarioEntity implements Serializable {

    @EmbeddedId
    private UsuarioEntityPK id;

    @OneToOne
    @MapsId("personaRelPKEntity")
    @JoinColumns({
        @JoinColumn(name="per_tipo_usu", referencedColumnName="per_tipo"),
        @JoinColumn(name="per_cod_usu", referencedColumnName="per_cod")
    })
    private PersonaRelEnity personaRelEntity = null;

    ...
}

@Embeddable
public class UsuarioEntityPK implements Serializable {

    // matches the PK type of PersonaRelEnity
    private PersonaRelPKEntity personaRelPKEntity;

    ...
}

派生标识在JPA2.1规范第2.4.1节中讨论。

 类似资料:
  • 我的数据库是: 我们可以在Table2中为Table1中的1设置多行。 我的TABLE1实体是: 我的Table1Id类是: 我的TABLE2实体是: 我的Table2Id类是: 当我尝试启动我的tomcat时,我有以下错误: 我尝试使用引用列,主键连接列和许多其他东西,但是通过在互联网上阅读它,它可以解决数据库建模问题。我认为问题是主键和外键在2个表中具有相同的名称,但我可能是错的......我

  • 问题内容: 我在为某些实体设置jpa映射时遇到麻烦。我有一个如下定义的父实体。 然后,我有一个带有复合键的子实体,以及此表的主键的外键,如下所示: 我知道子实体不正确,但是我不确定如何将其设置为具有复合PK。我知道我需要设置一个PK类,但是当一个字段是父类的外键时,我不确定该怎么做。一旦设置好,父级将如何引用子级实体? 任何帮助表示赞赏。 问题答案: 这受JPA 2规范的 第2.4.1节“与派生身

  • 我在jpa/Hibernate中映射复合键时遇到了问题。父实体和子实体都具有复合主键。 在运行时保存它时会出现以下异常: 我认为这是虚假的,因为有getter和setter。如果在priceRequestLegModel上使用mappedby=“leg”,在allocationModel上使用@mapsid,也会出现同样的错误。有人能指出我在这里做错了什么吗?

  • 我有2个表,即user和user_session。

  • 问题内容: 我在这里搜索,但未找到任何类似的主题,因此我发布了一个新问题。 我正在使用现有数据库上的Hibernate。我们不允许更改表的结构和数据。该应用程序正在从数据库读取数据,并根据某种逻辑迁移到另一个数据存储。 现在的问题是关于复合PK映射。例如 表A具有复合PK。 表B也有一个复合PK,此复合PK的一部分是A的PK,此处也用作FK。 我尝试了几种方法,但都无济于事。谁能告诉一个有效的Hi

  • 在child_tableparent_id列作为外键和复合主键的一部分。 Embedded类内部无法使用标识生成器。所以我在这里使用ID类。为child_id列生成自动增量值。 我无法将parent_table生成parent_id值作为外键值存储在child_table中,它存储为0。 有人能检查一下地图吗?帮帮我…… 提前致谢...