【问题标题】:How to store in one table an Entity composed by others Entity with Hibernate?如何在一张表中存储由其他实体与 Hibernate 组成的实体?
【发布时间】:2017-04-21 09:22:45
【问题描述】:

我正在使用 SpringMVC + Hibernate,但我遇到了一个难以解决的问题。 情况如下:

我有这个实体:

@Entity
@Table( name = "T_inscription" )
public class T_inscription implements Serializable {

  @Id
  @GeneratedValue( strategy = GenerationType.IDENTITY )
  private Long           id_inscription;

  @Valid
  private Identite       identite;

  @Valid
  private ParamConnexion paramConnexion;
//........
}

这门课:

public class Identite implements Serializable {

  @Size( min = 2 )
  @NotBlank
  @Pattern( regexp = "[\\p{L}\\p{Alpha} -]*" )
  @Column( name = "nom" )
  private String  nom;

  @Size( min = 2 )
  @NotBlank
  @Pattern( regexp = "[\\p{L}\\p{Alpha} -]*" )
  @Column( name = "prenom" )
  private String  prenom;
//....
}

还有这个:

public class ParamConnexion implements Serializable {

  @Pattern( regexp = "[\\p{L}\\p{Alnum}-_]*" )
  @Size( min = 2 )
  @Transient
  private String                 identifiant;

  @Pattern( regexp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\."
        + "[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*"
        + "@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?" )
  @Column( name = "email" )
  private String                 email;
//.....
}

我的表“T_inscription”包含以下字段:“id_inscription”、“nom”、“prenom”、“identifiant”、“email”等...

我的问题是,如何继续将“Identite”和“ParamConnexion”类的属性保存在“T_inscription”表中?

我不想为每个班级创建一个表格。有没有办法做到这一点 ?

我试过了,我得到了这个例外:Unknown column 'identite' in 'field list'

这很正常,因为我的表中没有这个字段,我不想创建它,我只想获取“Identite”的属性。

非常感谢。

【问题讨论】:

    标签: mysql hibernate spring-mvc hibernate-mapping


    【解决方案1】:

    这两个非实体类必须用@Embeddable注解。 identiteparamConnexion 字段必须使用 Embedded 进行注释,有关详细信息,请参阅这两个注释的 javadoc。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 2016-08-05
    • 2012-03-06
    • 2019-04-18
    • 2013-04-15
    • 2014-03-29
    • 1970-01-01
    相关资源
    最近更新 更多