Arturas M
Arturas M

Reputation: 4369

Is it possible to map different field types using hibernate?

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"/>

Upvotes: 1

Views: 833

Answers (1)

Manoj Kathiriya
Manoj Kathiriya

Reputation: 3416

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

Upvotes: 2

Related Questions