0

Is there any way to do something like this. I have an entity called person.

@Entity
@Table(name = "PERSON")
public class Person {
   @Column(name = "NAME", length = 128, columnDefinition = "VARCHAR2(128)")
   String name;
   @Column(name = "NATIONALITY", length = 128, columnDefinition = "VARCHAR2(128)")
   String nationality;
}

The 'NATIONALITY' column has numeric values - every number is a code of a country. I would like to map the 'nationality' field to my custom enum and create some method transforming code<->country.

@Entity
@Table(name = "PERSON")
public class Person {
   @Column(name = "NAME", length = 128, columnDefinition = "VARCHAR2(128)")
   String name;
   @Column(name = "NATIONALITY", length = 128, columnDefinition = "VARCHAR2(128)")
   Nationality nationality;
}

enum Nationality {
   USA, Brazil, Germany; // etc.
}

public static Nationality codeToNationality(String code);

Is there any hibernate methods for something like this? I can't modify columns definitions - the database is read only.

The second thing I'd like to achieve is to exclude any entities of unresolved nationality codes. The entities of codeToNationality(...) == null would be invisible to hibernate.

My model (persons, nationalities) is only an example.

Thanks for any help!

3 Answers 3

4

Both problems:

  1. mapping enum to some string in general and
  2. mapping unknown to null

can be solved via creating appropriate UserType.

Some example about enums and custom user type can be found from here. General description of custom types is part of Hibernate documentation.

Other option is to provide specific setters for enum, and these will convert to and from string value which is mapped to database column. This can be feasible if this custom type is needed only in this entity.

Sign up to request clarification or add additional context in comments.

1 Comment

This post's third solution shows the setter/getter method.
0

for your first problem have a look at the @Enumerated JPA Annotation, this will map your Strings to the enums. With a (EnumType.STRING) attribute you should be able to override the name() of the enum-values and return the Strings exactly as they are in the DB.

1 Comment

Otherwise fine idea, but you cannot override final method.
0

Ok, I've used UserType to convert String to Nationality.

public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
   String value = rs.getString(names[0]);
   // resolve nationality
   return /nationality found/ ? /Nationality object/ : null;
}

Now I have other problem. How to create Criteria for finding only resolved nationalities?

When I do

criteria.add(Restrictions.isNotNull("nationality"));

the criteria queries the String field, not Nationality field, which is always not null.

I understand that Criteria works on raw database (before transformation to java entities), so how can I get what I want - filter rows of unresolved nationalities?

1 Comment

This isn't an answer, but rather a new question!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.