如何在一个 table 中存储一个由其他实体与 Hibernate 组成的实体?
How to store in one table an Entity composed by others Entity with Hibernate?
我正在使用 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;
//........
}
这个class:
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;
//.....
}
我的 table "T_inscription" 包含这些字段:"id_inscription"、"nom"、"prenom"、"identifiant"、"email"、等...
我的问题是,如何继续在 table "T_inscription" 中保存 classes "Identite" 和 "ParamConnexion" 的属性?
我不想为每个 class 创建一个 table。有没有办法做到这一点 ?
我试过了,我得到了这个例外:Unknown column 'identite' in 'field list'
这很正常,因为我的table中没有这个字段,我不想创建它,我只想获取"Identite"的属性。
非常感谢。
两个非实体类必须用@Embeddable
注解。 identite
和 paramConnexion
字段必须用 Embedded
注释,请参阅这两个注释的 javadoc 以获取更多信息。
我正在使用 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;
//........
}
这个class:
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;
//.....
}
我的 table "T_inscription" 包含这些字段:"id_inscription"、"nom"、"prenom"、"identifiant"、"email"、等...
我的问题是,如何继续在 table "T_inscription" 中保存 classes "Identite" 和 "ParamConnexion" 的属性?
我不想为每个 class 创建一个 table。有没有办法做到这一点 ?
我试过了,我得到了这个例外:Unknown column 'identite' in 'field list'
这很正常,因为我的table中没有这个字段,我不想创建它,我只想获取"Identite"的属性。
非常感谢。
两个非实体类必须用@Embeddable
注解。 identite
和 paramConnexion
字段必须用 Embedded
注释,请参阅这两个注释的 javadoc 以获取更多信息。