简体   繁体   English

Hibernate注释OnetoOne关系

[英]Hibernate annotation OnetoOne relationship

I have a critical issue. 我有一个关键问题。 I have a table 我有桌子

TICKETINFO 票务信息

TICKETINFOID pk, REMARK varchar(128), TICKETDATE timestamp TICKETINFOID pk,备注varchar(128),TICKETDATE时间戳

it has a corresponding class with hibernate annotation which somewhat looks like this 它有一个带有休眠注释的对应类,看起来像这样

@Entity
@Table(name = "TICKETINFO")
public class Ticketinfo implements Serializable {

    @Id
    @Column(name = "TICKETINFOID")
    private Long id;
    @Column(name = "TICKETDATE")
    private String date;
    @column(name = "REMARK")
    private string remark;

    //getters and setters
}

now my work is that i need to create a child table of TICKETINFO table 现在我的工作是我需要创建一个TICKETINFO表的子表

TICKETINFO_REMARK TICKETINFO_REMARK

TICKETINFO_REMARK_ID pk, TICKETINFOID fk, REMARK varchar(128) TICKETINFO_REMARK_ID pk,TICKETINFOID fk,REMARK varchar(128)

and TICKETINFOID will be foreign key from TICKETINFO table and have to populate the REMARK field of TICKETINFO_REMARK along with the REMARK field of TICKETINFO for the corresponding TICKETINFOID. TICKETINFOID是TICKETINFO表中的外键,并且必须为相应的TICKETINFOID填充TICKETINFO_REMARK的REMARK字段以及TICKETINFO的REMARK字段。

For 1 TICKETINFOID there will be one REMARK and it could be null. 对于1个TICKETINFOID,将有一个REMARK,并且它可以为null。 The datatype of REMARK in Ticketinfo.java have to keep it as string.I can add extra logic but cannot change the existing flow. Ticketinfo.java中REMARK的数据类型必须保留为字符串。我可以添加额外的逻辑,但不能更改现有流程。

Please help me as I am in a terrible mess.... 请帮助我,因为我陷入了严重的混乱。

It sounds like a One-To-One would do the trick for the linking up of the child table. 听起来一对一会完成子表的链接。 For the remark itself, you'll probably want to hijack the accessors and mutators as I don't think Hibernate handles what you want out of the box. 对于备注本身,您可能要劫持访问器和更改器,因为我认为Hibernate不会开箱即用地处理您想要的东西。 If you have something as an attribute of another table, faking it this way isn't orthogonal or best practice. 如果您将某些内容作为另一个表的属性,则以这种方式进行伪造不是正交或最佳实践。 If you can't change it, leave it as it is unless you have a VERY good reason for writing weird fudges into your code. 如果您不能更改它,请保持原样,除非您有非常充分的理由将奇怪的软糖写入代码中。

You can make REMARK field as @Transient and use @PreUpdate @PrePersist and @PostLoad methods to load and save remark field from/to one-to-one mapped TICKETINFO_REMARK entity. 您可以将REMARK字段设置为@Transient,并使用@PreUpdate @PrePersist和@PostLoad方法将注释字段从一对一映射的TICKETINFO_REMARK实体加载和保存。 The same could be done on TICKETINFO_REMARK side. 可以在TICKETINFO_REMARK一侧执行相同的操作。

Here is quick example, it is not tested, just to give you an idea. 这是一个简单的示例,未经测试,只是为了给您一个想法。

@Entity
@Table(name = "TICKETINFO")
public class Ticketinfo implements Serializable {

    @Id
    @Column(name = "TICKETINFOID")
    private Long id;
    @Column(name = "TICKETDATE")
    private String date;
    @Transient
    private string remark;
    @OneToOne
    @PrimaryKeyJoinColumn
    private TicketinfoRemark ticketInfoRemark;

    @PostLoad
    public void postLoad() {
        if (ticketInfoRemark != null)
            this.remark = ticketInfoRemark.getRemark();
    }

    @PreUpdate
    @PrePersist
    public void prePersist() {
       if (ticketInfoRemark != null)
           ticketInfoRemark.setRemark(this.remark);
    }
    //getters and setters
}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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