1

だから、スイングアクションリスナーからのデータをSQLデータベースに入れようとしています。

これは、データベースへのデータの配置をトリガーするアクション リスナー コードの一部です。

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

これは DatabaseUtils の重要な部分です。

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

これは podaci.java で、すべてのデータを取得します。

@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)

データベースは次のようになります。

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

持続性:

    <?xml version="1.0" encoding="UTF-8"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> 
<persistence-unit name="HibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL"> 
<class>podaci.Podaci</class> 
<properties> 
<property name="hibernate.show_sql" value="true"/> 
<property name="hibernate.format_sql" value="true"/> 
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/> 
<property name="hibernate.connection.url" value="jdbc:h2:~/potrosnjaKalorija"/> 
<property name="hibernate.connection.username" value="seminar"/> 
<property name="hibernate.connection.password" value="seminar"/> 
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> 
<property name="hibernate.hbm2ddl.auto" value="update"/> 
</properties> 
</persistence-unit> 
</persistence>

多くのコードで申し訳ありませんが、アクションリスナーでそのファイルを実行すると、次のメッセージが表示されます。 org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387) の Podaci

等々...

私はデータベースの経験がありません.永続性がすべてがうまくいかなかったと思います.ypuが助けてくれることを願っています.

4

1 に答える 1

1

@Id プロパティ (brojKalorijaId) を生成するようにデータベースを構成しましたが、コンストラクターで設定しています。

コンストラクターを変更して、brojKalorijadId プロパティを設定しないようにします。エンティティが永続化されると、データベースは @Id の一意の値を作成します。

また

データベースで生成された @Id を保持する新しいプロパティをエンティティに追加します。

于 2013-06-27T01:32:38.317 に答える