Note that there are some explanatory texts on larger screens.

plurals
  1. POException in thread "AWT-EventQueue-0"; detached entity passed to persist (in Java)
    primarykey
    data
    text
    <p>So, I am trying to put my data from swing action listener in sql database.</p> <p>this is part of my action listener code that triggers putting data in database</p> <pre><code>poruka = "U suficitu ste ~ " + brojKalorija + " kalorija."; JOptionPane.showMessageDialog(null, poruka); Podaci noviPodaci = new Podaci(brojKalorija, danInt, mjesecInt, godinaInt, proteiniInt, ugljikohidratiInt, mastiInt, godineInt, masaInt, aktivnostInt, hrInt); DatabaseUtils.spremiPodatke(noviPodaci); } </code></pre> <p>This is important part of DatabaseUtils:</p> <pre><code>public class DatabaseUtils { public static void spremiPodatke(Podaci podaci) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("HibernatePersistenceUnit"); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); em.persist(podaci); em.getTransaction().commit(); } </code></pre> <p>This is podaci.java, it gets all data:</p> <pre><code>@Entity @Table(name = "podaci.podaci_izracuna") public class Podaci { @Id @Column(name = "brojKalorija_ID") @GeneratedValue private double brojKalorijaId; @Column(name = "dan") private int dan; . . . public Podaci(double brojKalorijaId, int dan, int mjesec, int godina, int proteini, int ugljikohidrati, int masti, int godine, int masa, int aktivnost, int hr) { this.brojKalorijaId = brojKalorijaId; this.dan = dan; this.mjesec = mjesec; this.godina = godina; this.proteini = proteini; this.ugljikohidrati = ugljikohidrati; this.masti = masti; this.godine = godine; this.masa = masa; this.aktivnost = aktivnost; this.hr = hr; } public double getBrojKalorijaId() { return brojKalorijaId; } public int getDan() { return dan; } . . (other getters) </code></pre> <p>Database looks like this:</p> <pre><code>create schema podaci; create table podaci.podaci_izracuna( brojKalorija_ID decimal (7,2) generated always as identity, dan int not null, mjesec int not null, godina int not null, proteini int not null, ugljikohidrati int not null, masti int not null, godine int, masa int, aktivnost int, heartRate int, primary key (brojKalorija_ID) ); </code></pre> <p>Persistence:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"&gt; &lt;persistence-unit name="HibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL"&gt; &lt;class&gt;podaci.Podaci&lt;/class&gt; &lt;properties&gt; &lt;property name="hibernate.show_sql" value="true"/&gt; &lt;property name="hibernate.format_sql" value="true"/&gt; &lt;property name="hibernate.connection.driver_class" value="org.h2.Driver"/&gt; &lt;property name="hibernate.connection.url" value="jdbc:h2:~/potrosnjaKalorija"/&gt; &lt;property name="hibernate.connection.username" value="seminar"/&gt; &lt;property name="hibernate.connection.password" value="seminar"/&gt; &lt;property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/&gt; &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;/persistence&gt; </code></pre> <p>Sorry for a lot of code, when I run that file with action listener I get this message: Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: podaci.Podaci at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)</p> <p>and so on...</p> <p>I have no experience in databases, I think persistence is where it all went wrong, hope ypu can help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload