使用嵌入对象覆盖外键列名

时间:2017-11-29 16:46:24

标签: java jpa

如果Embeddable对象已定义与实体的OneToOne关系。使用Embeddable对象的Entity如何覆盖关系的列名。

使用以下实体:

@Entity
public class User {

    @Id
    private int id;
    @Embedded
    private CubeLocation cubeLocation;
}

@Embeddable
public class CubeLocation { 
    @OneToOne
    private MailBox mailBox;            
    // .. other non-relevant fields
}

@Entity
public class MailBox {  
    @Id
    private String name;
    private String cell;
}

User实体的数据库表将包含列ID和MAILBOX_NAME。如何将数据库列的名称从MAILBOX_NAME更改为MBX_ID?

我曾尝试为AssociationOverride实体定义User注释,但在这种情况下我收到此错误:

    @Entity
    @AssociationOverride(
            name="cubeLocation.mailBox",
            joinColumns=@JoinColumn(name="MBX_ID"))
    public class User {
    }

错误:

  

org.hibernate.AnnotationException:非法尝试定义   带有mappedBy关联的@JoinColumn:cubeLocation.mailBox

1 个答案:

答案 0 :(得分:1)

关联覆盖应该继续嵌入:

@Entity
public class User {

    @Id
    private int id;

    @Embedded
    @AssociationOverride(
            name="mailBox",
            joinColumns=@JoinColumn(name="MBX_ID"))
    private CubeLocation cubeLocation;
}