Home @Embeddable with @ManyToOne
Reply: 1

@Embeddable with @ManyToOne

Code.IT
1#
Code.IT Published in 2018-01-11 11:03:34Z

Imagine following classes

@Embeddable
class A {
    @ManyToOne
    public B classB;

    ...
    public State someEnum;
}

@Entity
@Table(name = "TEST")
class B {
    public long id;
    //... some data


   @Embedded
   @AttributeOverrides({
        @AttributeOverride(
                name = "classB.id",
                column = @Column(name = "EMBEDDED1_ID")
        ),
        @AttributeOverride(
                name = "someEnum",
                column = @Column(name = "EMBEDDED1_SOMEENUM")
        )
   })
   public A embedded1;

   @Embedded
   @AttributeOverrides({
        @AttributeOverride(
                name = "classB.id",
                column = @Column(name = "EMBEDDED2_ID")
        ),
        @AttributeOverride(
                name = "someEnum",
                column = @Column(name = "EMBEDDED2_SOMEENUM")
        )
   })
   public A embedded2;
}

So I am expectingthe database table to look like

  • TEST
    • ID
    • ...
    • EMBEDDED1_ID
    • EMBEDDED1_SOMEENUM
    • EMBEDDED2_ID
    • EMBEDDED2_SOMEENUM

But if fails with:

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

Versions:

Hibernate: 5.2.12.FINAL JPA: hibernate-jpa-2.1-api

Simon Martinelli
2#
Simon Martinelli Reply to 2018-01-11 12:30:07Z

You cannot use @AttributeOverride to rename the foreign key column. You have to use @AssosiactionOverride

@Entity
@Table(name = "TEST")
public class B {

    public long id;

    @AssociationOverride(name = "classB", joinColumns = @JoinColumn(name = "EMBEDDED1_ID"))
    @AttributeOverrides({
            @AttributeOverride(
                    name = "someEnum",
                    column = @Column(name = "EMBEDDED1_SOMEENUM")
            )
    })
    @Embedded
    public A embedded1;

    @AssociationOverride(name = "classB", joinColumns = @JoinColumn(name = "EMBEDDED2_ID"))
    @AttributeOverrides({
            @AttributeOverride(
                    name = "someEnum",
                    column = @Column(name = "EMBEDDED2_SOMEENUM")
            )
    })
    @Embedded
    public A embedded2;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303554 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO