1

I have a strange problem with Hibernate. I can select something from database, but I can't insert or update any value. Here is my configuration and example code, persistance.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1_0.xsd"
    version="1.0">
    <persistence-unit name="UserDS">

        <class>****User</class>
        <properties>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/test"/>
            <property name="hibernate.connection.username" value="myuser"/>
            <property name="hibernate.connection.password" value="mypasword"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />

        </properties>
    </persistence-unit>
</persistence>

entity:

@Entity
@Table(name = "abc", schema = "public")
public class Abc {

    @Id
    private String aa;

    private Integer bb;

    public String getAa() {
        return aa;
    }

    public void setAa(String aa) {
        this.aa = aa;
    }

    public Integer getBb() {
        return bb;
    }

    public void setBb(Integer bb) {
        this.bb = bb;
    }
}

and DAO:

public class TestDOA{

    @PersistenceContext(unitName="UserDS")
    private EntityManager em;

    public String test() {          
        abc ab = (abc) em.createQuery("FROM abc").getResultList().get(0);
        ab.setBb(123);
        em.persist(ab);
        em.flush();
        return ab.getAa() + " " + ab.getBb();
    }

When I use test method, I get properly values. In server's log file I can see

[stdout] (default task-7) Hibernate: update public.abc set bb=? where aa=?

but there is no changes in my database. Where is the problem ?

6
  • 1
    You forgot you @transactional Commented Jul 6, 2014 at 16:47
  • 2
    The problem is most probably that you don't have any transaction being committed. Commented Jul 6, 2014 at 16:47
  • Ok, I added @Transactional additionality above test() method but It still doesn't work. Any ideas ? Commented Jul 6, 2014 at 17:41
  • I've just noticed that after calling em.persist() transaction is not blocked (I can't call update statement in pgadmin). I have to restart WildFly server to unlock transaction. I have no idea... Commented Jul 6, 2014 at 20:38
  • @Hannes Only if they're using Spring Framework, right? Commented Jul 7, 2014 at 0:59

1 Answer 1

4

Presumably the transaction isn't getting committed at any point. You can confirm this by examining pg_stat_activity on the database server - you'll see one or more <IDLE> in transaction entries for connections from your app server.

You need to decide if you're using JTA transactions or explicit transaction management.

If you're using Java EE 6 + JTA, you need to add appropriate annotations. Remember that JTA is only supported for EJBs and (in some app servers) CDI beans; a @TransactionManagement annotation silently has no effect on other objects.

If you're using Spring + JTA, you'll need to look up the docs for the @Transactional annotation and the rules around it instead.

If you're going to use explicit transaction management directly with the persistence provier you must begin and commit the transaction (the begin is implicit but it's better to write it explicitly) using em.getTransaction().begin(); and em.getTransaction().commit();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.