我正在制作一个网站,我试图在其中订购配料。我选择配料并输入其数量,然后单击添加。我重复了几次。然后我进入下一页,在其中我选择了一个供应商。然后我按“订单”。
预计将Nabavka插入数据库并打开一个页面,说明订单成功,但我收到一个异常,告诉我不能懒惰地初始化它。
来自NabavkaController。java:
@Autowired
NamirnicaRepository namr;
@Autowired
NabavkaRepository nabr;
@Autowired
DobavljacRepository dr;
@Autowired
NabavkaKorpaRepository nkr;...
@SuppressWarnings("unchecked")
@PostMapping(value = "sacuvajNabavku")
public String sacuvajNabavku(Dobavljac dobavljac, HttpServletRequest request) {
List<Namirnica> izabrane = (ArrayList<Namirnica>) request.getSession()
.getAttribute("izabrane");
List<Integer> brojevi = (ArrayList<Integer>) request.getSession()
.getAttribute("brojevi");
Nabavka n = new Nabavka();
for (int i = 0; i < izabrane.size(); i++) {
System.out.println(izabrane.get(i).getNaziv());
NabavkaKorpaPK nkPK = new NabavkaKorpaPK();
nkPK.setIdNabavka(n.getIdNabavka());
nkPK.setIdNamirnica(izabrane.get(i).getIdNamirnica());
NabavkaKorpa nk = new NabavkaKorpa();
nk.setId(nkPK);
nk.setKolicina(brojevi.get(i));
nk.setUkupnaCena(izabrane.get(i).getCenaNamirnice() * brojevi.get(0));
izabrane.get(i).addNabavkaKorpa(nk);
n.addNabavkaKorpa(nk);
nabr.updateNamirnica(izabrane.get(i));
}
n.setDatumNabavke(new Date());
n.setUkupnaCena(n.getNabavkaKorpas().stream().map(k -> k.getUkupnaCena()).reduce(0.0,
(a, b) -> a + b));
n.setDobavljac(dobavljac);
dobavljac.addNabavka(n);
nabr.save(n);
return "uspehNabavka";
}
Nabavka.java:
@Entity
@NamedQuery(name="Nabavka.findAll", query="SELECT n FROM Nabavka n")
public class Nabavka implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int idNabavka;
@Temporal(TemporalType.TIMESTAMP)
private Date datumNabavke;
private double ukupnaCena;
//bi-directional many-to-one association to Dobavljac
@ManyToOne
@JoinColumn(name="idDobavljac")
private Dobavljac dobavljac;
//bi-directional many-to-one association to Menadzer
@ManyToOne
@JoinColumns({
@JoinColumn(name="idMenadzer", referencedColumnName="idMenadzer"),
@JoinColumn(name="idRadnik", referencedColumnName="idRadnik")
})
private Menadzer menadzer;
//bi-directional many-to-one association to User
@ManyToOne
@JoinColumn(name="idUser")
private User user;
//bi-directional many-to-one association to NabavkaKorpa
@OneToMany(mappedBy="nabavka", cascade={CascadeType.ALL})
private List<NabavkaKorpa> nabavkaKorpas = new ArrayList<NabavkaKorpa>();
public Nabavka() {
}
public int getIdNabavka() {
return this.idNabavka;
}
public void setIdNabavka(int idNabavka) {
this.idNabavka = idNabavka;
}
public Date getDatumNabavke() {
return this.datumNabavke;
}
public void setDatumNabavke(Date datumNabavke) {
this.datumNabavke = datumNabavke;
}
public double getUkupnaCena() {
return this.ukupnaCena;
}
public void setUkupnaCena(double ukupnaCena) {
this.ukupnaCena = ukupnaCena;
}
public Dobavljac getDobavljac() {
return this.dobavljac;
}
public void setDobavljac(Dobavljac dobavljac) {
this.dobavljac = dobavljac;
}
public Menadzer getMenadzer() {
return this.menadzer;
}
public void setMenadzer(Menadzer menadzer) {
this.menadzer = menadzer;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
public List<NabavkaKorpa> getNabavkaKorpas() {
return this.nabavkaKorpas;
}
public void setNabavkaKorpas(List<NabavkaKorpa> nabavkaKorpas) {
this.nabavkaKorpas = nabavkaKorpas;
}
public NabavkaKorpa addNabavkaKorpa(NabavkaKorpa nabavkaKorpa) {
getNabavkaKorpas().add(nabavkaKorpa);
nabavkaKorpa.setNabavka(this);
return nabavkaKorpa;
}
public NabavkaKorpa removeNabavkaKorpa(NabavkaKorpa nabavkaKorpa) {
getNabavkaKorpas().remove(nabavkaKorpa);
nabavkaKorpa.setNabavka(null);
return nabavkaKorpa;
}
}
纳米尔尼卡。java:
@Entity
@NamedQuery(name="Namirnica.findAll", query="SELECT n FROM Namirnica n")
public class Namirnica implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int idNamirnica;
private double cenaNamirnice;
private String kolicina;
private String naziv;
private int stanje;
//bi-directional many-to-one association to NabavkaKorpa
@OneToMany(mappedBy="namirnica")
private List<NabavkaKorpa> nabavkaKorpas = new ArrayList<NabavkaKorpa>();
public Namirnica() {
}
public int getIdNamirnica() {
return this.idNamirnica;
}
public void setIdNamirnica(int idNamirnica) {
this.idNamirnica = idNamirnica;
}
public double getCenaNamirnice() {
return this.cenaNamirnice;
}
public void setCenaNamirnice(double cenaNamirnice) {
this.cenaNamirnice = cenaNamirnice;
}
public String getKolicina() {
return this.kolicina;
}
public void setKolicina(String kolicina) {
this.kolicina = kolicina;
}
public String getNaziv() {
return this.naziv;
}
public void setNaziv(String naziv) {
this.naziv = naziv;
}
public int getStanje() {
return this.stanje;
}
public void setStanje(int stanje) {
this.stanje = stanje;
}
public List<NabavkaKorpa> getNabavkaKorpas() {
return this.nabavkaKorpas;
}
public void setNabavkaKorpas(List<NabavkaKorpa> nabavkaKorpas) {
this.nabavkaKorpas = nabavkaKorpas;
}
public NabavkaKorpa addNabavkaKorpa(NabavkaKorpa nabavkaKorpa) {
getNabavkaKorpas().add(nabavkaKorpa);
nabavkaKorpa.setNamirnica(this);
return nabavkaKorpa;
}
public NabavkaKorpa removeNabavkaKorpa(NabavkaKorpa nabavkaKorpa) {
getNabavkaKorpas().remove(nabavkaKorpa);
nabavkaKorpa.setNamirnica(null);
return nabavkaKorpa;
}
}
纳巴夫卡科帕。java:
@Entity
@NamedQuery(name="NabavkaKorpa.findAll", query="SELECT n FROM NabavkaKorpa n")
public class NabavkaKorpa implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private NabavkaKorpaPK id;
private int kolicina;
private double ukupnaCena;
//bi-directional many-to-one association to Nabavka
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="idNabavka", insertable = false, updatable = false)
private Nabavka nabavka;
//bi-directional many-to-one association to Namirnica
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="idNamirnica", insertable = false, updatable = false)
private Namirnica namirnica;
public NabavkaKorpa() {
}
public NabavkaKorpaPK getId() {
return this.id;
}
public void setId(NabavkaKorpaPK id) {
this.id = id;
}
public int getKolicina() {
return this.kolicina;
}
public void setKolicina(int kolicina) {
this.kolicina = kolicina;
}
public double getUkupnaCena() {
return this.ukupnaCena;
}
public void setUkupnaCena(double ukupnaCena) {
this.ukupnaCena = ukupnaCena;
}
public Nabavka getNabavka() {
return this.nabavka;
}
public void setNabavka(Nabavka nabavka) {
this.nabavka = nabavka;
}
public Namirnica getNamirnica() {
return this.namirnica;
}
public void setNamirnica(Namirnica namirnica) {
this.namirnica = namirnica;
}
}
纳巴夫卡科尔帕普。java:
@Embeddable
public class NabavkaKorpaPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(insertable=false, updatable=false)
private int idNamirnica;
@Column(insertable=false, updatable=false)
private int idNabavka;
public NabavkaKorpaPK() {
}
public int getIdNamirnica() {
return this.idNamirnica;
}
public void setIdNamirnica(int idNamirnica) {
this.idNamirnica = idNamirnica;
}
public int getIdNabavka() {
return this.idNabavka;
}
public void setIdNabavka(int idNabavka) {
this.idNabavka = idNabavka;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof NabavkaKorpaPK)) {
return false;
}
NabavkaKorpaPK castOther = (NabavkaKorpaPK)other;
return
(this.idNamirnica == castOther.idNamirnica)
&& (this.idNabavka == castOther.idNabavka);
}
public int hashCode() {
final int prime = 31;
int hash = 17;
hash = hash * prime + this.idNamirnica;
hash = hash * prime + this.idNabavka;
return hash;
}
}
NabavkaKorpaRepository。java:
public interface NabavkaKorpaRepository extends JpaRepository<NabavkaKorpa, NabavkaKorpaPK> {
}
NabavkaRepository。java:
public interface NabavkaRepository extends JpaRepository<Nabavka, Integer>, NabavkaRepoSpecific {
}
NabavkaRepoSpeicific.java
public interface NabavkaRepoSpecific {
public void updateNamirnica(Namirnica namirnica);
}
NabavkaRepoSpecificImpl。java:
@Repository
@Transactional
public class NabavkaRepoSpecificImpl implements NabavkaRepoSpecific {
@PersistenceContext
EntityManager em;
@Override
public void updateNamirnica(Namirnica namirnica) {
try {
em.merge(namirnica);
} catch (Exception e) {
e.printStackTrace();
}
}
}
NamirnicaRepository。java:
public interface NamirnicaRepository extends JpaRepository<Namirnica, Integer> {
}
例外情况:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: model.Namirnica.nabavkaKorpas, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:614) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:218) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:591) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.write(AbstractPersistentCollection.java:415) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
at org.hibernate.collection.internal.PersistentBag.add(PersistentBag.java:407) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
at model.Namirnica.addNabavkaKorpa(Namirnica.java:85) ~[classes/:na]
at com.example.demo.controller.NabavkaController.sacuvajNabavku(NabavkaController.java:95) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.21.jar:5.3.21]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.21.jar:5.3.21]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.21.jar:5.3.21]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.21.jar:5.3.21]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.21.jar:5.3.21]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.21.jar:5.3.21]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.21.jar:5.3.21]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.21.jar:5.3.21]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.21.jar:5.3.21]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.21.jar:5.3.21]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) ~[tomcat-embed-core-9.0.64.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.21.jar:5.3.21]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.64.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.64.jar:9.0.64]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.21.jar:5.3.21]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.21.jar:5.3.21]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.21.jar:5.3.21]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.21.jar:5.3.21]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.21.jar:5.3.21]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.21.jar:5.3.21]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1787) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
错误的原因是您得到了一个列表
默认情况下,此列表是延迟的。这意味着您只能在一个点访问它(当持久上下文/Hibernate会话打开时)。
@OneToMany(mappedBy="namirnica")
private List<NabavkaKorpa> nabavkaKorpas = new ArrayList<NabavkaKorpa>();
即使在B点打开一个新事务也无济于事,因为这将是另一个持久的上下文/Hibernate会话(与a点不同)。
您可以尝试制作nabavkaKorpas
EAGER
,可能会有所帮助。但是你有一个设计问题。
通用规则
切勿使用OneToMany部分中的关联集合。
如何修复
您必须保存一个新的NabavkaKorpa,而不是Namirnica。
NabavkaKorpa nabavka = new NabavkaKorpa();
nabavka.setNamirnica(namirnica);
nabavkaKorpaRepositiry.save(nabavka);
你甚至不需要Namirnica
来做到这一点。
用于Hibernate
Namirnica namirnica= new Namirnica();
namirnica.setId(namirnicaId);
NabavkaKorpa nabavka = new NabavkaKorpa();
nabavka.setNamirnica(namirnica);
nabavkaKorpaRepositiry.save(nabavka);
对于Spring Data(没有额外的查询从数据库中获取Namirnica)
Namirnica namirnica= namirinicaRepositiry.getOne(namirnicaId);
NabavkaKorpa nabavka = new NabavkaKorpa();
nabavka.setNamirnica(namirnica);
nabavkaKorpaRepositiry.save(nabavka);
笔记
拥有CascadeType是一个非常糟糕的主意。所有
在这里。
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="idNamirnica", insertable = false, updatable = false)
private Namirnica namirnica;
我不明白为什么会发生这种情况。根据代码路径,当引发此异常时,我应该在同一线程中并且会话应存在。 有人能告诉我我错过了什么吗? 我有设置 在hibernate.cfg.xml档案里 我在Servlet过滤器中创建了以下代码 在index.xhtml文件中,我有以下调用: 做一些事情 index.xhtml正在使用使用ui include加载menu.xhtml文件的模板。然后,菜单文件会插入menu
我有一个应用程序,我正在扩展它以提供REST API。在主站点中一切正常,但当我尝试访问REST API时,我在异常日志中得到以下内容: 禁用延迟加载将解决此问题,但会导致不可接受的性能(加载时间从 200 毫秒到 22 秒)。我不知道如何处理这个问题。 我刚开始在ColdFusion中Rest,在我看来,CFC正在以一种不寻常的方式被处理。它们似乎没有被初始化(init方法似乎没有运行),现在看
我正在使用JPA(Hibernate 4 . 3 . 3作为持久性提供者)和Spring (3.2.2),我所有的字段都加载得很好,但是当我试图访问我的集合时,它抛出了错误- 当我调试这个时,我的实体类中定义的每个集合都有错误- 我尝试使用collection.size()和Hibernate.initialize(),但都不起作用。在Internet上搜索时,我发现扩展Persitence将解决
问题内容: 我使用休眠来创建一个REST API。我创建了一种获取表中所有项目的方法。 这是我的Language.java 这是我的Patient.java 重要提示:Bean和映射是通过NetBeans从MySQL数据库反向工程的。调用时,不需要获取任何与之相关的数据。我的表格只有2列,而。表的前键为 在rest api中使用此方法之前,它没有任何异常即可完美运行。但是,当我在rest api中
问题内容: 我有下一个错误: 我的实体: 我有一个服务班: 我从另一种服务方法调用服务: 但是,当我试图调用这个方法我收到线异常,当我打电话时,也会发生同样的异常上。我已经检查了事务管理器,并且配置正确,并且此步骤中存在事务。另外,我已经检查了关于与此异常相关的stackoverflow的大量答案,但没什么用。 可能是什么原因造成的? 问题答案: 看来模型是一个独立的实体。 尝试合并并在合并实例上
问题内容: 我使用hibernate创建一个REST API。我创建了一种获取表中所有项目的方法。 这是我的Language.java 这是我的Patient.java 重要提示:Bean和映射是通过NetBeans从MySQL数据库反向工程的。调用时,不需要获取任何与之相关的数据。我的表格只有2列,而。表的前键为 在rest api中使用此方法之前,它没有任何异常即可完美运行。但是,当我在res