Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate - how to JoinColumn an EmbeddedId

Tags:

java

hibernate

Situation: I have a masterDB and a library1DB and library2DB.

library1DB and library2DB are two separate database but has the same schema because each library must have their own database. Now we have a masterDB, this is an aggregated version of all the data in all libraries (library1DB and library2DB), still taking note of their respective ids and mapping them to their library id.

Here's I want my tables to be structured: book - book_id - library_id - title - shelf_id shelf - shelf_id - library_id - book_id - description

I have these models:

@Entity
public class Book {

    @EmbeddedId
    private BookKey bookKey;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({
            @JoinColumn(name = "SHELF_ID", referencedColumnName = "SHELF_ID"),
            @JoinColumn(name = "LIBRARY_ID", referencedColumnName = "LIBRARY_ID")
    })
    private ObjectA objectA;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({
            @JoinColumn(name = "ANOTHER_ID", referencedColumnName = "ANOTHER_ID"),
            @JoinColumn(name = "LIBRARY_ID", referencedColumnName = "LIBRARY_ID")
    })
    private ObjectB objectB;    

    @Column
    private String title;

}

@Embeddable
public class BookKey implements Serializable {
    @Column(name = "BOOK_ID")
    private long bookId;
    @Column(name = "LIBRARY_ID")
    private long libraryId;
}

But I get this exception:

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

I've tried using @IdClass instead of @Embeddable and @EmbeddedId, and I got this:

Caused by: org.hibernate.DuplicateMappingException:  Table [book] contains physical column name [libraryId] represented by different logical column names: [libraryId], [LIBRARY_ID]

Any help?

Thanks!

like image 723
lorraine Avatar asked Oct 29 '14 09:10

lorraine


People also ask

Do you need JoinColumn?

It is not necessary to have @JoinColumn annotation. You can always override it. If you won't provide it in your code then Hibernate will automatically generate one for you i.e. default name for your column.

What is referencedColumnName in @JoinColumn?

The referencedColumnName attribute tells Hibernate the name of the database column it shall use as the foreign key.

What is @JoinColumn?

Annotation Type JoinColumn. @Target(value={METHOD,FIELD}) @Retention(value=RUNTIME) public @interface JoinColumn. Specifies a column for joining an entity association or element collection. If the JoinColumn annotation itself is defaulted, a single join column is assumed and the default values apply.

What is @embeddedid?

The @Embeddable annotation is used to model persistent objects that have no identity of their own, because they are nested inside another entity. Parent topic: Adding a primary key to the JPA entity. Last updated: 2022-05-27.


1 Answers

You should add insert="false", update="false" for the second mapped column library_id. Try this:

@JoinColumn(name = "LIBRARY_ID", referencedColumnName = "LIBRARY_ID",
            insertable = false, updatable = false)
like image 51
Alex Avatar answered Oct 21 '22 04:10

Alex