0

smalltalk "Hibernate + ZK", got an exception

asked 2008-08-14 17:44:32 +0800

mh1 gravatar image mh1
15

Hi,

i followed the smalltalk "Hibernate + ZK" and tried to do so. But I got an exception in the saveOrUpdate-Method of my DAO-Object:

14.08.2008 19:29:08 org.zkoss.zk.ui.impl.UiEngineImpl handleError:890
SCHWERWIEGEND: >>org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction
>> at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
>> at $Proxy7.saveOrUpdate(Unknown Source)
>> at entities.dao.UserDaoImpl.saveOrUpdate(UserDaoImpl.java:74)
>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>> at java.lang.reflect.Method.invoke(Method.java:597)
>> at bsh.Reflect.invokeMethod(Unknown Source)
>> at bsh.Reflect.invokeObjectMethod(Unknown Source)
>>...

i got it work, when i do

_session.beginTransaction();
_session.saveOrUpdate(myUser);
_session.getTransaction().commit();

instead of only

_session.saveOrUpdate(myUser);


When i understand the article right, the transaction should be handled automatically?! Or didn't i understand the article right?


here is my code, maybe something in the hibernate.cfg.xml is not ok?

Thanks for help!

Here is the hibernate.cfg.xml and the code causing this problems:

hibernate.cfg.xml:
---------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
<!-- connnect via JNDI -->
<property name="hibernate.connection.datasource">java:MySqlDS</property>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">pizza</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

<!-- Patch the HSQL error for batching
<property name="hibernate.jdbc.batch_size">0</property> -->

<!-- Hibernate's transaction factory -->
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>

<!-- Mapping persistence classes -->
<mapping class="entities.User"/>

</session-factory>

</hibernate-configuration>
-----------------------------------------------------------

User.java:
-----------------------------------------------------------
package entities;

import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@SuppressWarnings("serial")
@Entity
public class User
implements java.io.Serializable {

public User() {
super();
}

public User(String name, String password, String email) {
super();
this.email = email;
this.name = name;
this.password = password;
}

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(length=50,nullable=false)
private String name;
@Column(length=30,nullable=false)
private String email;
@Column(length=30,nullable=false)
private String password;



public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}


}
-----------------------------------------------------------


UserDaoImpl:
-----------------------------------------------------------
package entities.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.hibernate.Session;
import org.zkoss.zkplus.hibernate.HibernateUtil;

//import entities.JpaUtil;
import entities.User;

public class UserDaoImpl {

Session _session;

public UserDaoImpl() {
_session = HibernateUtil.getSessionFactory().getCurrentSession();
}

public void saveOrUpdate(User myUser) {
//_session.beginTransaction();
_session.saveOrUpdate(myUser);
//_session.getTransaction().commit();
}

}
-----------------------------------------------------------

userRegistration.zul
-----------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<p:zk xmlns:p="http://www.zkoss.org/2005/zul" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd ">
<p:window id="win" title="Benutzer registrieren" border="normal" width="500px" sizable="false">
<p:zscript>
class MyButton extends Button {
public void onClick(MouseEvent event) {
import entities.User;
import entities.dao.UserDaoImpl;
User myUser = new User("UserReg1", "123", "[email protected]");
UserDaoImpl myUserDao = new UserDaoImpl();
myUserDao.saveOrUpdate(myUser);
}
}
</p:zscript>
<grid width="90%">
<rows>
<row>
<p:button label="add User" use="MyButton"></p:button>
</row>
</rows>
</grid>
</p:window>
</p:zk>
----------------------------------------------------------------

Bold Text

Source Code
Source Code
Source Code

delete flag offensive retag edit

1 Reply

Sort by ยป oldest newest

answered 2008-08-15 08:20:38 +0800

mh1 gravatar image mh1
15

I've solved the problem!

I had to ad

<listener>
<description>Hibernate "Open Session In View" Session Lifecycle</description>
<listener-class>org.zkoss.zkplus.hibernate.OpenSessionInViewListener</listener-class>
</listener>

too in the zk.xml-File

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2008-08-14 17:44:32 +0800

Seen: 257 times

Last updated: Aug 15 '08

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More