收到org.hibernate。MappingException:复制属性映射

时间:2022-04-28 19:12:41

I'm attempting to build an application level view with Spring JPA. I'm also using lombok.

我正在尝试使用Spring JPA构建一个应用程序级视图。我也使用lombok。

There is an existing client already using this web service which can't be changed at this time. We have a new client which needs the same data and even more now. So I thought this could be addressed with an application level view so to speak.

有一个已经在使用这个web服务的现有客户端,此时不能更改它。我们有一个新客户需要同样的数据,甚至更多。所以我认为这可以通过应用程序级视图来解决。

The basic scope of the problem is I have 3 entities: A, B and C.

问题的基本范围是我有三个实体:A、B和C。

A and C are entities pointed at the same table. C has more properties than A.
Both C and A have references to B.

A和C是指向同一个表的实体。C有比A更多的属性,C和A都有对B的引用。

@Entity
@Getter
@Setter
@Table(name="Foo", schema="dbz")
public class A {
  @Id
  @Column(name="FOO_I")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int id;

  @OneToMany(fetch =FetchType.EAGER, cascade= CascadeType.ALL)
  @JoinColumn(name="FOO_I",nullable=false)
  private Set<B> items = new HashSet<B>();

  @Column(name="X")
  private String x;
}

@Entity
@Getter
@Setter
@Table(name="Bar", schema="dbz")
public class B {
  @Id
  @Column(name="BAR_I")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int id;

  @Column(name="Y")
  private String y;

  @Column(name="Z")
  private int z;
}


@Entity
@Getter
@Setter
@Table(name="Foo", schema="dbz")
public class C {
  @Id
  @Column(name="FOO_I")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int id;

  @OneToMany(fetch =FetchType.EAGER, cascade= CascadeType.ALL)
  @JoinColumn(name="FOO_I",nullable=false)
  private Set<B> items = new HashSet<B>();

  @Column(name="X")
  private String x;

  @Column(name="MoreData")
  private String moreData;

  //And much more other data...
}

When I comment out the @Entity and @Table on class C and rebuild, I don't get an exception in my validation tests.

当我在类C上注释出@Entity和@Table并重新构建时,我的验证测试中没有异常。

When I leave the @Entity and @Table annotations on class C, I get the following exception:

当我将@Entity和@Table注释放在类C上时,我得到以下异常:

Caused by: org.hibernate.MappingException: Duplicate property mapping of _items_FOO_IBackref found in com.acme.Bar

引起的:org.hibernate。MappingException:在com.ac . bar中找到的_items_FOO_IBackref的重复属性映射

If I rename items to bars in C, then I get the following exception:

如果我将项目重命名为C中的bar,则会出现以下异常:

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.acme.Bar column: FOO_I (should be mapped with insert="false" update="false")

引起的:org.hibernate。MappingException:实体:com.acme映射中的重复列。Bar列:FOO_I(应该映射为insert="false" update="false")

Is there any way to get hibernate to accept 2 java classes backed by the same table which relate to another entity? I'm not sure if I am missing something here or if this is a framework limitation.

有没有办法让hibernate接受与另一个实体相关的同一个表支持的两个java类?我不确定我是否漏掉了什么或者这是一个框架限制。

1 个解决方案

#1


1  

You are using component mapping so use @Embedded annotation for the A class without using @Entity, @Id and @Table annotations here is a useful link;

您正在使用组件映射,所以在A类中使用@Embedded annotation,而无需使用@Entity、@Id和@Table注释,这是一个有用的链接;

http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-component-using-annotations-1.html

http://www.dzone.com/tutorials/java/hibernate/hibernate - example/hibernate映射组件-使用注释- 1. - html

#1


1  

You are using component mapping so use @Embedded annotation for the A class without using @Entity, @Id and @Table annotations here is a useful link;

您正在使用组件映射,所以在A类中使用@Embedded annotation,而无需使用@Entity、@Id和@Table注释,这是一个有用的链接;

http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-component-using-annotations-1.html

http://www.dzone.com/tutorials/java/hibernate/hibernate - example/hibernate映射组件-使用注释- 1. - html