简体   繁体   中英

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:

I read many posts with my same problem, but none seems to fix my issue.

If I add @Cascade(CascadeType.ALL) to the Parent Object (menu) I get the error above.

If I add @Cascade(CascadeType.ALL) to the Child Object (VoceMenu) I get this other error:

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

I can't understand if the problem depends on the way I bound the 2 entities or depends on something else.

This is my Menu entity

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;
    }



}

This is my VoceMenu Entity

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 you for you help

The org.hibernate.TransientObjectException: object references an unsaved transient error happens as the menu is referring to the child(voce_menu) record that is not created yet in the database.

You have to modify the SovuMenu Entity relationship to be only:

@ManyToOne     
@JoinColumn(name="voci_menu_id") 
private Menu menu;  

and modify the menu to be:
@OneToMany(cascade = CascadeType.ALL) 
@joinColumn(name="menu_id") 
private List<VoceMenu> voceMenuList;

Make sure that you import all annotations from the Javax.persistence

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM