Hibernate不会将数据插入到Derby数据库行中 [英] Hibernate not inserting data into Derby database rows

查看:181
本文介绍了Hibernate不会将数据插入到Derby数据库行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用eclipse和Jboss 6.0服务器以及德比嵌入式驱动程序。
我创建了一个数据库:

  CREATE TABLE QUOTAZIONI.QuotazioniBancarie 

Cliente VARCHAR 50)NOT NULL,
Prodotto VARCHAR(50)NOT NULL,
Prezzo DOUBLE NOT NULL,
Data DOUBLE NOT NULL,
CONSTRAINT primary_key PRIMARY KEY(Cliente,Prodotto,Prezzo,数据)
);

并使用与之关联的实体的JPA工具:Quotazionibancarie.java和QuotazionibancariePK.java 2个文件导致它是一个复合主键)

保留行的代码片段如下所示:

 


@PersistenceContext(unitName =P1Server)
private EntityManager em;



EntityTransaction et = em.getTransaction();
et.begin();
Quotazionibancarie q1 =新的Quotazionibancarie(q.getCliente(),q.getProdotto(),q.getPrezzo());
em.persist(q1);
et.commit();

其中q class是我为实用工具制作的类。
数据字段是自动生成的。



当我尝试保存数据库中的内容时,服务器不会给我任何错误,但不会保存行。

  11:36:02,710 INFO [STDOUT] Hibernate:
11:36:02,710 INFO [STDOUT]插入
11:36:02,711 INFO [STDOUT]到
11:36:02,711 INFO [STDOUT] Quotazionibancarie
11:36:02,711 INFO [STDOUT](cliente,data, PRODOTTO)
11:36:02,711 INFO [STDOUT] values
11:36:02,711 INFO [STDOUT](?,?,?,?)

我在论坛上搜索,但没有设法找到适合我的问题的任何解决方案。
任何有关导致我的问题以及如何解决问题的想法?

persistence.xml:

 <?xml version =1.0encoding =UTF-8?> 
< persistence version =2.0xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-实例xsi:schemaLocation =http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd\">
< persistence-unit name =P1Servertransaction-type =JTA>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< jta-data-source> java:/ Quotazioni< / jta-data-source>
< class> entities.Quotazionibancarie< / class>
< class> entities.QuotazionibancariePK< / class>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.DerbyDialect/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.format_sqlvalue =true/>
< property name =hibernate.hbm2ddl.autovalue =create/>
< / properties>
< / persistence-unit>
< /余辉>

quotazioni-ds.xml

 <?xml version =1.0encoding =UTF-8?> 
< datasources>
< local-tx-datasource>
<! - 数据源的jndi名称,前缀为java:/ - >
< jndi-name> Quotazioni< / jndi-name>
< driver-class> org.apache.derby.jdbc.EmbeddedDriver< / driver-class>
< connection-url> jdbc:derby:// localhost:1527 / Quotazioni; create = true< / connection-url>
<用户名>根< /用户名>
<密码>根< /密码>
< min-pool-size> 5< / min-pool-size>
< max-pool-size> 20< / max-pool-size>
< idle-timeout-minutes> 5< / idle-timeout-minutes>
< track-statements />
<取决于> jboss:service = Derby< / depends>
< / local-tx-datasource>

< mbean code =org.jboss.jdbc.DerbyDatabasename =jboss:service = Derby>
< attribute name =Database> Quotazioni< / attribute>
< / mbean>

< / datasources>

告诉我是否需要指定其他信息。




Stefano

解决方案

不成功保存数据的一些常见原因:


  1. 您实际上从数据库中收到错误,但您没有检查错误或以某种方式忽略它。

  2. 您已将您的应用程序设置为autocommit = false,但从未提交过,因此数据库正在回退您的更改。

  3. 实际上您正在保存数据,但是当您去寻找它时,您没有在正确的位置查找(错误的数据库或数据库中的错误模式),因此您在查找时没有看到数据。

还有其他一些原因,但这三个要先检查。


I'm using eclipse with Jboss 6.0 server and the derby embedded driver. I created a database:

CREATE TABLE QUOTAZIONI.QuotazioniBancarie
(
Cliente VARCHAR(50) NOT NULL,
Prodotto VARCHAR(50) NOT NULL,
Prezzo DOUBLE NOT NULL,
Data DOUBLE NOT NULL,
CONSTRAINT primary_key PRIMARY KEY (Cliente, Prodotto, Prezzo, Data)
);

and using the JPA tools a entity associated to it: Quotazionibancarie.java and QuotazionibancariePK.java (there are 2 files cause it's a composite primary key)

The code fragment to persist the rows is the following:

.
.
.
@PersistenceContext(unitName = "P1Server")
private EntityManager em;
.
.
.
EntityTransaction et = em.getTransaction();
et.begin();
Quotazionibancarie q1 = new Quotazionibancarie(q.getCliente(), q.getProdotto(),        q.getPrezzo());          
em.persist(q1);
et.commit();

where the q class is a class I made for utility. The Data field is automatically generated.

When I try to save the content in the DB, the server doesn't give me any error but doesn't save the rows either.

11:36:02,710 INFO  [STDOUT] Hibernate: 
11:36:02,710 INFO  [STDOUT]     insert 
11:36:02,711 INFO  [STDOUT]     into
11:36:02,711 INFO  [STDOUT]         Quotazionibancarie
11:36:02,711 INFO  [STDOUT]         (cliente, data, prezzo, prodotto) 
11:36:02,711 INFO  [STDOUT]     values
11:36:02,711 INFO  [STDOUT]         (?, ?, ?, ?)

I searched on the forums but didn't manage to find any solution fit for my issue. Any idea of what causes my problem and how to fix it?

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="P1Server" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/Quotazioni</jta-data-source>
        <class>entities.Quotazionibancarie</class>
        <class>entities.QuotazionibancariePK</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
        </properties>       
    </persistence-unit>
</persistence>

quotazioni-ds.xml

<?xml version="1.0" encoding="UTF-8" ?>
<datasources>
<local-tx-datasource>
    <!-- The jndi name of the DataSource, it is prefixed with java:/ -->
    <jndi-name>Quotazioni</jndi-name>
    <driver-class>org.apache.derby.jdbc.EmbeddedDriver</driver-class>
    <connection-url>jdbc:derby://localhost:1527/Quotazioni;create=true</connection-url>
    <user-name>root</user-name>
    <password>root</password>
    <min-pool-size>5</min-pool-size>
    <max-pool-size>20</max-pool-size>
    <idle-timeout-minutes>5</idle-timeout-minutes>
    <track-statements />
    <depends>jboss:service=Derby</depends>
</local-tx-datasource>

<mbean code="org.jboss.jdbc.DerbyDatabase" name="jboss:service=Derby">
    <attribute name="Database">Quotazioni</attribute>
</mbean>

</datasources>

Tell me if I need to specify some other informations.

Thx. Stefano

解决方案

Some common reasons for not successfully saving the data:

  1. You're actually getting an error returned from the database, but you're not checking for the error or are ignoring it somehow.
  2. You've set your application to autocommit=false, but never committed, so the database is rolling back your changes
  3. You are in fact saving the data, but when you go to look for it, you're not looking in the right place (wrong database, or the wrong schema inside the database), so you don't see the data when you go to look for it.

There are other reasons, but those are three good ones to check first.

这篇关于Hibernate不会将数据插入到Derby数据库行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆