当前位置: 首页 > 知识库问答 >
问题:

Hibernate错误消息:在刷新前保存瞬态实例

孙佐
2023-03-14

在尝试创建一对多关系时,我将“保存Flushing之前的瞬态实例”错误消息usign Spring JPA connection to PostgreSQL。“Alquiler”(一个)是我实体的名字,另一个是“Peliculas”(对许多人来说)

package com.Project.Movies.model;

import java.util.Date;
import java.util.List;

import javax.persistence.*;

@Entity
@Table(name = "Alquiler")
public class Alquiler {
@Id
@SequenceGenerator(
        name = "alquiler_seq",
        sequenceName = "alquiler_seq",
        allocationSize = 1
        )
@GeneratedValue(
        strategy = GenerationType.SEQUENCE,
        generator = "alquiler_seq"
        )

private Long id;

int dias;
float costopordia;
String formapago;
Date fechaAlquiler;

@Column
@ElementCollection(targetClass=Pelicula.class)
private List<Pelicula> peliculas;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id")
private Socio socio;


public Alquiler() {
    super();
}

public Alquiler(int dias, float costopordia, String formapago, Date fechaAlquiler, List<Pelicula> pelicula, Socio socio) {
    super();
    this.dias = dias;
    this.costopordia = costopordia;
    this.formapago = formapago;
    this.fechaAlquiler = fechaAlquiler;
    this.peliculas = pelicula;
    this.socio = socio;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public Date getFechaAlquiler() {
    return fechaAlquiler;
}

public void setFechaAlquiler(Date fechaAlquiler) {
    this.fechaAlquiler = fechaAlquiler;
}

@OneToMany(targetEntity=Pelicula.class, cascade = CascadeType.ALL,orphanRemoval = true)
public List<Pelicula> getPeliculas() {
    return peliculas;
}

public void setPeliculas(List<Pelicula> pelicula) {
    this.peliculas = pelicula;
}

public Socio getSocio() {
    return socio;
}

public void setSocio(Socio socio) {
    this.socio = socio;
}

boolean CrearAlquiler() {
    return true;
}

public int getDias() {
    return dias;
}

public void setDias(int dias) {
    this.dias = dias;
}

public float getCostopordia() {
    return costopordia;
}

public void setCostopordia(float costopordia) {
    this.costopordia = costopordia;
}

public String getFormapago() {
    return formapago;
}

public void setFormapago(String formapago) {
    this.formapago = formapago;
}
package com.Project.Movies.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AlquilerConfig {
@Bean
CommandLineRunner commandLineRunnerA(IAlquilerRepository ialquilerRepository)
{
    
return args ->{
    
    List<Pelicula> myList = new ArrayList<Pelicula>();
    
    Pelicula shawshank = new Pelicula("The Shawshank Redemption",1994,"2hr 22mins","Two imprisoned men bond over a number of years, finding solace and eventual redemption through...","Frank Darabont","B","Drama",3);
    Pelicula inception = new Pelicula("Inception",2010,"2hrs 28mins","Dom Cobb es un ladrón con una extraña habilidad para entrar a los sueños de la gente y robarles los secretos de sus subconscientes","Christopher Nolan","C","Science Fiction",4);
    Pelicula fightclub = new Pelicula("Fight Club",1999,"2hrs 19mins","Un empleado de oficina insomne, harto de su vida, se cruza con un vendedor peculiar...","David Fincher","C","Suspenso/Drama",2);                 
    
    myList.add(inception);
    
    myList.add(shawshank);
    myList.add(inception);
    myList.add(fightclub);
    
    Socio mariana= new Socio("Mariana Navidad","Arcos #34, Los Arcos","4446784563");
    Socio sebastian= new Socio("Sebastian Mendoza","Rios #38, Mares","4336784563");
    Socio roberto= new Socio("Roberto Obregon","Estrella #89, Universo","4556784563");
    
    Alquiler shawshankAlquiler = new Alquiler(3,30,"efectivo",new Date(),myList,mariana);
    Alquiler inceptionAlquiler = new Alquiler(5,25,"tarjeta",new Date(),myList,sebastian);
    Alquiler inceptionAlquiler2 = new Alquiler(5,25,"efectivo",new Date(),myList,roberto);
    
    ialquilerRepository.saveAll(List.of(shawshankAlquiler,inceptionAlquiler,inceptionAlquiler2));
};  
}

}

错误信息

java.lang.IllegalStateException:在org.springframework.boot.springapplication.CallRunner(springapplication.java:794)~[spring-boot-2.5.6.jar:2.5.6]在org.springframework.boot.springapplication.callRunners(springapplication.java:775)~[spring-boot-2.5.6.jar:2.5.6]在org.springframework.boot.springapplication.callRunners()~[spring-boot-2.5.6.jar:2.5.6]在org.springframework.boot.springapplication.run(springapplication.java:1332)~[spring-boot-2.5.6.jar:2.5.6]在com.univaProject.univaMovies.univaMoviesApplication.main(univaMoviesApplication.java:11)~[classs/:na]由:org.springframework.dao.invaliddataAccessapiusageException:嵌套异常是java.lang.IllegalStateException:org.hibernate.transientObjectException:object引用了一个未保存的瞬态实例-在org.springframework.orm.jpa.EntityManagerFactorYutils.convertJPA.EntityManagerFactorYutils.convertJPAAccessExceptionIfprobility之前保存瞬态实例:com.univaProject.univaMovies.model.pelicula(在.jar:5.3.12]在org.springframework.orm.jpa.jpatransactionManager.doCommit(jpatransactionManager.java:566)~[spring-orm-5.3.12.jar:5.3.12]在org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:711)~[spring-tx-5.3.12.jar:5.3.12]在rorg.springframework.transaction.interceptor.transactionAspectSupport.CommitTransactionAfterreturn(TransactionAspectSupport.java:654)~[spring-tx-5.3.12.jar:5.3.12]在framework.aop.framework.reflectiveMethodInvocation.proce(reflectiveMethodInvocation.java:186)~[spring-aop-5.3.12.jar:5.3.12]在org.springframework.dao.support.persistenceExceptionTranslationInterceptor.invoke(persistenceExceptionTranslationInterceptor.java:137)~[spring-tx-5.3.12.jar:5.3.12]在pository.support.crudmethodmetadatapostprocessor$crudMethodMetadataPopulatingMethodInterceptor.invoke(crudMethodMetadatapostProcessor.java:174)~[spring-data-jpa-2.5.6.jar:2.5.6]在org.springframework.aop.framework.reflectiveMethodInvocation.proced(reflectiveMethodInvocation.java:186)~[spring-aop-5.3.12.jar:5.3.12]在.reflectiveMethodInvocation.proce(reflectiveMethodInvocation.java:186)~[spring-aop-5.3.12.jar:5.3.12]在org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:215)~[spring-aop-5.3.12.jar:5.3.12]在com.sun.proxy.$proxy87.saveAll(来源未知)~[na:na]在.boot.springapplication.CallRunner(Springapplication.java:791)~[spring-boot-2.5.6.jar:2.5.6]...5个公共框架被省略

共有1个答案

齐晟
2023-03-14

您说您添加了级联类型。但是您没有将其添加到pelicula关系中。当您尝试保存alquilerHibernate发现peliculas没有保存,并且由于它没有cascadetype,所以它提供了这个异常。

把它贴成这样

@OneToMany(cascade = CascadeType.PERSIST)
private List<Pelicula> peliculas;
 类似资料:
  • 错误是:by:javax.el.elexception://jsf/submit.xhtml@20,76 listener=“#{banklocationmb.savelocation}”对象引用了一个未保存的瞬态实例-在刷新之前保存瞬态实例 尝试添加级联持久化,我收到以下错误 org.springframework.orm.jpa.jpasystemexception:org.hibernate

  • 对象引用未保存的瞬态实例 - 在刷新错误之前保存瞬态实例 我有 3 个实体。我将使用一对多关系映射彼此实体。 产品收费详情有 燃料类型有 计算类型具有 我有一个问题,当我保存产品时,它抛出这样一个错误。我试着用@ManyToOne(cascade = CascadeType。ALL)到productChargeDetail,但是当我保存一个产品时,它显示一个来自应用程序的错误复制错误。FuelTy

  • 对象引用未保存的临时实例-在刷新之前保存临时实例。 我有一个2类组织实体和应用实体。组织和应用程序有多对多的关系。 组织实体 申请实体有 组织运作impl。JAVA 我的问题是,我是否使用applicationRepository保存applicationEntity。save(applicationEntity)也会像我的cascade类型一样保存组织。所有的一切都在现场定义。我犯了这个错误。

  • 错误: 我还尝试了,但是 病因是什么?显然,我设置了nullable=true(在数据库中也是如此),尽管我有这个错误。有什么想法吗?

  • 我正在使用Seam Framework。我有2个实体: Request.java 和请求事件.java 当我进行此交易时: 我得到了这个错误:

  • 我目前正在做一个项目,我遇到了这个错误: 发生了什么:1。)我在登录后设置了一个会话范围变量,比如SessionScopeVariableA。 2.)然后我有一个页面,我在其中添加一个实体,比如EntityA。 3.)EntityA有一个惰性字段sessionScopeVariableA,所以当我调用add方法时,我必须设置这个变量。 4.)请注意,SessionScopeVariableA被包装