How to use custom type in JPA column?

后端 未结 2 1926
别那么骄傲
别那么骄傲 2021-01-18 02:54

I have a class:

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


        
相关标签:
2条回答
  • 2021-01-18 03:36

    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)

    0 讨论(0)
  • 2021-01-18 03:44

    You can make email an entity and it will work...but it's pretty ineficcient.

    @Entity
    public class Email {
      ...
    }
    

    Or you can swtich from Email to String and it will work. (What's the point of wrapping a String anyway?)

    You can read this tutorial about custom user types in Hibernate (since you tagged it).

    Or you can use @Embebbed as Bozho says.

    0 讨论(0)
提交回复
热议问题