Is it possible to map different field types using hibernate?

我是研究僧i 提交于 2019-12-13 02:59:26

问题


I'm wondering if it's possible to map different data types. For instance, I have a Visit data type in Java, which has a userEmail field which is of the type String, and can I map it with hibernate to a database table which would have userID instead of userEmail? And according to the id it would find the email and return to java the email and vice versa? Is this possible?

If yes, then how would the mapping look like? Or how is this supposed to look like:

<many-to-one name="patientEmail" 
    column="idPatient" 
    not-null="true" 
    class="com.my.myapp.datamodel.Patient"/>

回答1:


Yes, you can.

You have to create one class which extend to UserType of Hibernate.

Custom UserType Hibernate

It can be user like :

@Column(name = "commission_type")
        @Type(type = "com.core.commission.model.FNEnumUserType", parameters = @Parameter(name = "type", value = "com.core.commission.dto.CommissionType"))
        private CommissionType commissionType;

Where FNEnumUserType is cstom data type same like you have Visit



来源:https://stackoverflow.com/questions/14071417/is-it-possible-to-map-different-field-types-using-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!