'How to set the schema of @OneToMany autogenerated tables?
I want to put all of the following autogenerated tables into a specific schema.
@Entity
@Table(name = "master_table", schema = "test")
public class MasterTable {
@OneToMany
private List<VideoEntity> videos;
@Entity
@Table(name = "video_entity", schema = "test")
public static class VideoEntity {
}
}
Result: there are the two entity tables in test schema, but also one in the public schema called master_table_videos for the list mapping.
Question: how can I tell hibernate to also put the list-mapping table in the same schema than the others?
Solution 1:[1]
I think you should use the @JoinTable annotation, at least that allows to set the schema name in standard JPA. Check the JavaDoc for Java EE 7 or Java EE 6.
So it would be something like @JoinTable(name = "master_to_videos", schema = "test" ), and you could also specify the name of the join column if required.
Solution 2:[2]
Hibernate will create the table in whichever persistence.xml the entity is defined in. So if MasterTable and VideoEntity are both in persistence.xml, it will create both tables in the configured data schema.
Solution 3:[3]
I agree with Hein Blöd i tested the @joinTable annotation after any other annotation like @ManyToOne @OneToMany ... as for your example it becomes like this
@OneToMany
@JoinTable(schema = "testSchema" )
private List<VideoEntity> videos;
testSchema is interpreted by Hibernate as test-schema
i know this is for an old question i'm writing this so that any one right now can find the correct answer i search the internet and this is the first question i found.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | teacurran |
| Solution 3 | Aymen B'm |
