简体   繁体   中英

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?

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.

Hibernate将在定义实体的任何persistence.xml中创建表。因此,如果MasterTable和VideoEntity都在persistence.xml中,它将在配置的数据模式中创建这两个表。

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 inte.net and this is the first question i found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM