【问题标题】:'one to many' attribute value type should not be 'persistence entity'“一对多”属性值类型不应是“持久性实体”
【发布时间】:2013-05-06 05:32:45
【问题描述】:

我有 1 个用户访问多个其他域,并将其放入我的代码中:

用户类

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

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

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID_USUARIO",unique = true,nullable = false)
    private Long id;

    @NotBlank(message = "usuario.cadastro.nome.obrigatorio")
    @Column(name = "NOME", unique = true, nullable = false,length = 100)
    private String nome;

    @Email(message = "usuario.cadastro.email.invalido")
    @Column(name = "E_MAIL", unique = true, nullable = false,length = 100)
    private String email;

    @NotBlank(message = "usuario.cadastro.senha.obrigatoria")
    @Column(name = "SENHA", unique = true, nullable = false, length = 10)
    private String senha;

    @OneToMany(mappedBy = "usuario",fetch = FetchType.LAZY)
    @Cascade({CascadeType.ALL})
    @Fetch(value = FetchMode.SELECT)
    private Set<DiasUsuarioTimeSheet> diasUsuarioTimeSheetList;


    public Usuario() {
        diasUsuarioTimeSheetList = new LinkedHashSet<DiasUsuarioTimeSheet>();
    }

另一个类:

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "DIAS_USUARIO_TIMESHEET")
public class DiasUsuarioTimeSheet {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID_DIAS_USUARIO_TIMESHEET",unique = true,nullable = false)
    private Long id;

    @Column(name = "DIAS_ID_DIAS", unique = true, nullable = false)
    private Dias dia;

    @ManyToOne
    @Cascade({CascadeType.MERGE, CascadeType.SAVE_UPDATE})
    @Column(name = "USUARIO_ID_USUARIO", unique = true, nullable = false)
    private Usuario usuario;

    @Column(name = "TIME_SHEET_ID_TIME_SHEET", unique = true, nullable = false)
    private TimeSheet timeSheet;

    public DiasUsuarioTimeSheet() {
    }

我有错误:

@OneToMany(mappedBy = "usuario",fetch = FetchType.LAZY)
@Cascade({CascadeType.ALL})
@Fetch(value = FetchMode.SELECT)
private Set<DiasUsuarioTimeSheet> diasUsuarioTimeSheetList;

“一对多”属性值类型不应该是“持久性实体”

【问题讨论】:

    标签: hibernate


    【解决方案1】:

    以下注释

    @Column(name = "USUARIO_ID_USUARIO", unique = true, nullable = false)
    

    应该是

    @JoinColumn(name = "USUARIO_ID_USUARIO", nullable = false)
    

    unique=true 属性没有意义,因为它是多对一关联:您显然会有多个引用同一用户的 DiasUsuarioTimeSheets。

    同理,下面的注解

    @Column(name = "TIME_SHEET_ID_TIME_SHEET", unique = true, nullable = false)
    

    必须替换为

    @ManyToOne
    @JoinColumn(name = "TIME_SHEET_ID_TIME_SHEET", nullable = false)
    

    【讨论】:

      【解决方案2】:

      将此添加到您的 hibernate.cfg.xml 文件中

      <mapping class="com.company.models.DiasUsuarioTimeSheet"/>
      

      【讨论】:

        猜你喜欢
        • 2015-11-14
        • 2020-03-28
        • 2020-07-03
        • 2021-07-13
        • 2014-04-21
        • 1970-01-01
        • 1970-01-01
        • 2018-03-31
        • 1970-01-01
        相关资源
        最近更新 更多