Hibernate not inserting data into Derby database rows

送分小仙女□ 提交于 2019-12-11 07:52:24

问题


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


回答1:


your table is Quotazionibancarie or QuotazioniBancarie? may be that was the problem




回答2:


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.



来源:https://stackoverflow.com/questions/9259639/hibernate-not-inserting-data-into-derby-database-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!