我读了许多同样问题的帖子,但似乎没有一个能解决我的问题。
如果我将@Cascade(CascadeType.ALL)
添加到父对象(菜单),我会收到上述错误。
如果我将@Cascade(CascadeType.ALL)
添加到子对象(VoceMenu),我会收到另一个错误:
ERROR 2015-11-22 15:43:53,689 [http-bio-8080-exec-8] org.hibernate.util.JDBCExceptionReporter - Column 'title' cannot be null
ERROR 2015-11-22 15:43:53,689 [http-bio-8080-exec-8] com.springgestioneerrori.DAO.MenuDAO - could not insert: [com.springgestioneerrori.model.Menu]
ERROR 2015-11-22 15:43:53,691 [http-bio-8080-exec-8] org.hibernate.util.JDBCExceptionReporter - Column 'title' cannot be null
ERROR 2015-11-22 15:43:53,692 [http-bio-8080-exec-8] org.springframework.transaction.interceptor.TransactionInterceptor - Application exception overridden by commit exception
我无法理解问题是否取决于我绑定2个实体的方式或取决于其他内容。
这是我的菜单实体
package com.springgestioneerrori.model;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
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 javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.stereotype.Component;
@Component
@Entity
@Table(name="menu")
public class Menu implements Serializable{
private static final long serialVersionUID = -7161291305179518214L;
public Menu() {
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", nullable=false, updatable=false)
private int id;
@NotBlank
@NotNull
@NotEmpty
@Size(min=3,max=50)
@Pattern(regexp = "^[A-Za-z0-9 ]*$")
@Column(name="titolo")
private String titolo;
@NotBlank
@NotNull
@NotEmpty
@Size(min=3,max=255)
@Pattern(regexp = "^[A-Za-z0-9 ]*$")
@Column(name="title")
private String title;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "menu")
@Cascade(CascadeType.ALL) <--------------------
private List<VoceMenu> voceMenuList;
@OneToMany(mappedBy="menu", fetch=FetchType.EAGER)
private Set<MenuAutorizzazioni> menuAutorizzazioni;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitolo() {
return titolo;
}
public void setTitolo(String titolo) {
this.titolo = titolo;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<VoceMenu> getVoceMenuList() {
return voceMenuList;
}
public void setVoceMenuList(List<VoceMenu> voceMenuList) {
this.voceMenuList = voceMenuList;
}
public void setMenuAutorizzazioni(Set<MenuAutorizzazioni> menuAutorizzazioni) {
this.menuAutorizzazioni = menuAutorizzazioni;
}
public Set<MenuAutorizzazioni> getMenuAutorizzazioni() {
return menuAutorizzazioni;
}
//transient///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Transient
@NotBlank
@NotNull
@NotEmpty
@Size(min=3,max=255)
@Pattern(regexp = "^[A-Za-z0-9 ]*$")
private String currentLanguageTitle;
@Transient
@NotBlank
@NotNull
@NotEmpty
@Size(min=3,max=255)
@Pattern(regexp = "^[A-Za-z0-9 ]*$")
private String currentLanguageTitolo;
public String getCurrentLanguageTitle() {
return currentLanguageTitle;
}
public void setCurrentLanguageTitle(String currentLanguageTitle) {
this.currentLanguageTitle = currentLanguageTitle;
}
public String getCurrentLanguageTitolo() {
return currentLanguageTitolo;
}
public void setCurrentLanguageTitolo(String currentLanguageTitolo) {
this.currentLanguageTitolo = currentLanguageTitolo;
}
}
这是我的VoceMenu实体
package com.springgestioneerrori.model;
import java.io.Serializable;
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.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.stereotype.Component;
@Component
@Entity
@Table(name="voci_menu")
public class VoceMenu implements Serializable{
private static final long serialVersionUID = 141763842624541637L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id", unique = true, nullable = false)
private Integer id;
@NotBlank
@NotEmpty
@NotNull
@Column(name="descrizione", nullable=false)
private String descrizione;
@NotBlank
@NotEmpty
@NotNull
@Column(name="title")
private String title;
@NotBlank
@NotEmpty
@NotNull
@Column(name="url")
private String url;
/*@NotBlank
@NotEmpty
@NotNull(message="Call is invalid.")*/
@Column(name="ordine")
private int ordine;
@ManyToOne(targetEntity=Menu.class)
@JoinColumn(name="menu", referencedColumnName="id")
@Cascade(CascadeType.ALL) <--------------------
private Menu menu;
@OneToOne(targetEntity=Autorizzazione.class)
@JoinColumn(name="autorizzazione", referencedColumnName="id_autorizzazione")
private Autorizzazione autorizzazione;
public VoceMenu() {
}
public String getDescrizione() {
return descrizione;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void setDescrizione(String descrizione) {
this.descrizione = descrizione;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getOrdine() {
return ordine;
}
public void setOrdine(int ordine) {
this.ordine = ordine;
}
public Menu getMenu() {
return menu;
}
public void setMenu(Menu menu) {
this.menu = menu;
}
public Autorizzazione getAutorizzazione() {
return autorizzazione;
}
public void setAutorizzazione(Autorizzazione autorizzazione) {
this.autorizzazione = autorizzazione;
}
//transient ////////////////////////////////////////////////////////////////////////////////////////////////////////
@Transient
private String currentLanguageDescrizione;
@Transient
private String currentLanguageTitle;
public String getCurrentLanguageDescrizione() {
return currentLanguageDescrizione;
}
public void setCurrentLanguageDescrizione(String currentLanguageDescrizione) {
this.currentLanguageDescrizione = currentLanguageDescrizione;
}
public String getCurrentLanguageTitle() {
return currentLanguageTitle;
}
public void setCurrentLanguageTitle(String currentLanguageTitle) {
this.currentLanguageTitle = currentLanguageTitle;
}
}
Tahnk你的帮助