Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom type in JPA column?

I have a class:

public class Email {
  private String name;
  private String domain;
  public String toString() {
    return name + "@" + domain;
  }  
}

I want to use it in JPA column:

@Entity
public class User {
  @Id private Integer id;
  private Email email;
}

This is what Hibernate says:

org.hibernate.MappingException: Could not determine type for: com.XXX.Email

How to make it understand my custom type. I think that it's something very simple, but can't find in documentation.

like image 705
yegor256 Avatar asked Sep 30 '10 07:09

yegor256


1 Answers

Well, there are a number of ways:

  • annotate the Email class with @Embeddable, and have:

     @Embedded
     private Email email;
    
  • declare a custom value type - see here (using @Type)

like image 188
Bozho Avatar answered Sep 21 '22 04:09

Bozho