Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Hibernate to call my custom typedef?

I'm trying to define a CompositeUserType to handle a specific type in my JPA/Hibernate app. I have a CompositeUserType called ApplicationMessageType that is designed to handle my mapping.

According to what I've read, I should be able to create a package-info.java class in my domain hierarchy that contains the TypeDefs. Mine looks like this:

@TypeDefs({
    @TypeDef(
        defaultForType = ApplicationMessage.class,
        typeClass = ApplicationMessageType.class
    )
})
package mptstp.domain;

import mptstp.domain.messages.ApplicationMessage;

import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

If I understand correctly, the fact that I'm using the defaultForType parameter to the TypeDef, anytime I attempt to save or load an ApplicationMessage, the custom type code should be called.

I've set breakpoints on every method within the ApplicationMessageType class, and none of them ever get called.

Does anyone have any idea where I've gone wrong? The code compiles, but it appears that the TypeDef annotations never got called to register the ApplicationMessageType.

Any pointers would be greatly appreciated...

Thanks

like image 915
Steve Avatar asked Sep 28 '10 18:09

Steve


People also ask

What Hibernate mapping types?

These types are “clob”, “blob”, “binary”, “text” etc. Clob and blob data types are present to maintain the data type mapping of large objects like images and videos.

What is @type in hibernate?

@Type annotation is for hibernate i.e. to tell what type of data do you want to store in database. Let's take a simple example: @Type(type="yes_no") private boolean isActive; Here return type is boolean but the value which gets stored in the database will be in Y or N format instead of true / false .

What is Hibernate mapping?

hibernate mappings are one of the key features of hibernate . they establish the relationship between two database tables as attributes in your model. that allows you to easily navigate the associations in your model and criteria queries.

What are Hibernate Annotations?

Hibernate annotations are the newest way to define mappings without the use of XML file. You can use annotations in addition to or as a replacement of XML mapping metadata. Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping.


1 Answers

I'm trying to define a CompositeUserType to handle a specific type in my JPA/Hibernate app. I have a CompositeUserType called ApplicationMessageType that is designed to handle my mapping.

Keep in mind that you'll have to declare the columns (using the @Columns annotation) at the field or property level for your composite user type.

According to what I've read, I should be able to create a package-info.java class in my domain hierarchy that contains the TypeDefs.

This is correct and this is the recommended strategy when the custom type is used in more than one entity.

If I understand correctly, the fact that I'm using the defaultForType parameter to the TypeDef, anytime I attempt to save or load an ApplicationMessage, the custom type code should be called.

In theory, the defaultForType element means that when Hibernate encounters a property of class ApplicationMessage, it is supposed to delegate the persistence strategy to the custom mapping type ApplicationMessageType.

But I'm sure how this works with a composite user type though, I'm not sure if you'll have to repeat the @Type to declare the @Columns (in which case, you should declare a name alias in the TypeDef).

Does anyone have any idea where I've gone wrong? The code compiles, but it appears that the TypeDef annotations never got called to register the ApplicationMessageType..

Can't say. Did you actually succeed to persist an entity with a property of class ApplicationMessage or not at all? Maybe show an annotated entity.

Reference

  • 2.4.3.2. Type

Resources

  • Hibernate CompositeUserType and Annotations
like image 119
Pascal Thivent Avatar answered Oct 09 '22 13:10

Pascal Thivent