@Embedded列表对象休眠

时间:2016-03-27 10:58:20

标签: java hibernate list

我需要帮助嵌入带有Hibernate的列表对象。

这是实体消息:

@Embeddable
public class Mensaje implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String mensaje;

    private Usuario emisor;

    private Usuario receptor;

    //??????????????????????
    private Calendar ficha;

    private boolean leido;

}

这是实体用户:

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

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID_USUARIO")
    private int idUsuario;

    @Column(name = "USERNAME", unique = true, nullable = false)
    private String username;

    @Column(name = "PASSWORD", nullable = false)
    private String password;

    @Column(name = "CORREO", nullable = false)
    private String correo;

    @OneToMany(mappedBy ="usuario",cascade= CascadeType.ALL)
    private List<Curso> cursos;

    @Column(name = "PAIS")
    private String pais;

    @Column(name = "CIUDAD")
    private String ciudad;

    //????????????????????????????
    @Embedded
    List <Mensaje> mensajes;
}

我喜欢用Hibernate在实体Usuario中嵌入实体Mensaje。 我尝试过使用ElementoColection,但是没有一个好的结果。 你能帮我吗? 谢谢;)

2 个答案:

答案 0 :(得分:1)

请对此进行测试

@Entity
public class Employee {
  ....
  ....
  @ElementCollection
  @CollectionTable(
        name = "MENSAJE",
        joinColumns = @JoinColumn(name="EMPLOYEE_ID")
  )
  private List<Mensaje> mensajes;
  ....
  ....
}
@Embeddable
public class Mensaje implements Serializable{
  ....
  ....
  @Column 
  @Temporal(TemporalType.TIMESTAMP)
  private Calendar ficha;
  ....
  ....
}

答案 1 :(得分:0)

地址可嵌入对象的示例

@Embedded
@AttributeOverrides({
    @AttributeOverride(name = "streetAndNumber", column = @Column(name = "ADDRESS_STREET_AND_NUMBER")),
    @AttributeOverride(name = "addition", column = @Column(name = "ADDRESS_ADDITION")),
    @AttributeOverride(name = "postalCode", column = @Column(name = "ADDRESS_POSTALCODE", length = 32)),
    @AttributeOverride(name = "city", column = @Column(name = "ADDRESS_CITY", length = 256)),
    @AttributeOverride(name = "country", column = @Column(name = "ADDRESS_COUNTRY", length = 64))})
private Address address;