'一对多'属性值类型不应该是'持久性实体' [英] 'one to many' attribute value type should not be 'persistence entity'
本文介绍了'一对多'属性值类型不应该是'持久性实体'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
User class
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 =
@Column(name =ID_USUARIO,unique = true,nullable = false)
私有长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)
私人字符串电子邮件;
@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)
私人套餐< DiasUsuarioTimeSheet> diasUsuarioTimeSheetList;
$ b $ 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 =
@Column(name =ID_DIAS_USUARIO_TIMESHEET,unique = true,nullable = false)
private长ID;
@Column(name =DIAS_ID_DIAS,unique = true,nullable = false)
private Dias dia;
$ b @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)
私人TimeSheet timeSheet;
public DiasUsuarioTimeSheet(){
}
我有错误在:
@OneToMany(mappedBy =usuario,fetch = FetchType.LAZY)
@Cascade({CascadeType .ALL})
@Fetch(value = FetchMode.SELECT)
private Set< DiasUsuarioTimeSheet> diasUsuarioTimeSheetList;
'一对多'属性值类型不应该是'持久性实体'
解决方案
以下注释
@Column =USUARIO_ID_USUARIO,unique = true,nullable = false)
应该是
@JoinColumn(name =USUARIO_ID_USUARIO,nullable = false)
unique = true
属性没有意义,因为它是一个ManyToOne关联:显然,有几个DiasUsuarioTimeSheets引用相同用户。
同样,以下注释
@Column(name =TIME_SHEET_ID_TIME_SHEET,unique = true,nullable = false)
必须替换为
$ $ $ $ $ $
@JoinColumn(name =
I have 1 user to many another domain and put this in my code:
User class
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>();
}
Another class:
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() {
}
I have error in:
@OneToMany(mappedBy = "usuario",fetch = FetchType.LAZY)
@Cascade({CascadeType.ALL})
@Fetch(value = FetchMode.SELECT)
private Set<DiasUsuarioTimeSheet> diasUsuarioTimeSheetList;
'one to many' attribute value type should not be 'persistence entity'
解决方案
The following annotation
@Column(name = "USUARIO_ID_USUARIO", unique = true, nullable = false)
should be
@JoinColumn(name = "USUARIO_ID_USUARIO", nullable = false)
the unique=true
attribute doesn't make sense, since it's a ManyToOne association: you'll obviously have several DiasUsuarioTimeSheets referencing the same user.
Similarly, the following annotation
@Column(name = "TIME_SHEET_ID_TIME_SHEET", unique = true, nullable = false)
must be replaced by
@ManyToOne
@JoinColumn(name = "TIME_SHEET_ID_TIME_SHEET", nullable = false)
这篇关于'一对多'属性值类型不应该是'持久性实体'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文