-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Read-only EntityManager
PostPosted: Wed Feb 21, 2007 5:08 am 
Newbie

Joined: Wed Feb 21, 2007 4:45 am
Posts: 3
Hi,
I have tested successfully Hibernate (v3.2.2) without EntityManager (using Session class). But, when trying to use EntityManger it seems not to insert/update any field.
The code is:

EntityManagerFactory emf = Persistence .createEntityManagerFactory("manager1");
EntityManager em = emf.createEntityManager();

Plane plane = new Plane("Boeing56", 85);
plane.setPassangers("90");
em.persist(plane);
em.flush();

em.close();
emf.close();

When I call the entityManager.flush() it does not insert any field and it throws:
Exception in thread "main" javax.persistence.TransactionRequiredException: no transaction is in progress

If I don't call flush() then the insert is not done too.

I have tried to get a field and then update it. This field was successfully found (Plane plane = em.find( Plane.class, "Boeing210" ); ) but I can not update it. This object was persisted in DDBB with save method of Session class in another code.

I am using:
Hibernate: 3.2.2
HSQLDB (server mode) 1.8.0

Here you have the full config and code:
/* resumed Plane class */
package airline;
public class Plane {
String name = new String();
int passangers = 0;
//getters, setters and consturctor
}

/* persistence.xml */
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
<class>airline.Plane</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost/xdb"/>
<property name="hibernate.max_fetch_depth" value="3"/>
</properties>
</persistence-unit>
</persistence>

/* plane.hbm.xml */
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="airline">
<class name="Plane" table="PLANES">
<id name="name" column="NAME"/>
<property name="passangers" column="PASSANGERS"/>
</class>
</hibernate-mapping>

/* SQL to create the plane table */
CREATE TABLE PLANES (
NAME VARCHAR(20) NOT NULL ,
PASSANGERS INTEGER NULL,
PRIMARY KEY(NAME)
);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 6:52 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes, this is part of the spec

Code:
* @throws TransactionRequiredException if there is
*                 no transaction
* @throws PersistenceException if the flush fails
*/
public void flush();


Updating outside a transaction is evil, start a transaction em.getTransaction().begin()

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.