如何在JPA列中使用自定义类型? [英] How to use custom type in JPA column?

查看:81
本文介绍了如何在JPA列中使用自定义类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个班级:

 公共类电子邮件{
私人字符串名称;
私人字符串域;
public String toString(){
return name +@+ domain;


$ / code $ / pre

我想在JPA列中使用它: p>

  @Entity 
public class User {
@Id private Integer id;
私人电子邮件电子邮件;

这就是Hibernate所说的:

  org.hibernate.MappingException:无法确定类型:com.XXX.Email 

如何让它理解我的自定义类型。我认为这很简单,但在文档中找不到。

解决方案

好的,有很多方法:
$ b


  • 使用 @注释电子邮件可嵌入的,并有:

      @Embedded 
    私人电子邮件电子邮件;


  • 声明自定义值类型 - 请参阅 here (使用 @Type ) p>



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.

解决方案

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)

这篇关于如何在JPA列中使用自定义类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆