Hello,
I am a newbie in Hibernate and I have a question.
I divided my application in 2 layers: DAO layer and Service layer.
A class from my DAO Layer:
Code:
public class SchuelerDaoImpl implements SchuelerDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public Schueler persistOrMerge(Schueler schueler) {
Schueler s = (Schueler) this.sessionFactory.getCurrentSession().merge(schueler);
return s;
}
}
A class from my service layer:
Code:
public class SchuelerServiceImpl implements SchuelerService {
private SchuelerDao schuelerDao;
public void setSchuelerDao(SchuelerDao schuelerDao) {
this.schuelerDao = schuelerDao;
}
@Override
@Transactional
public Schueler createUser(Schueler schueler) {
Schueler s = schuelerDao.persistOrMerge(schueler);
//s.setName("neuer name");
return s;
}
}
And my main() method:
Code:
public static void main(String[] args) {
ApplicationContext beanFactory = new FileSystemXmlApplicationContext("./src/resources/applicationContext.xml");
SchuelerService schuelerService = (SchuelerService)beanFactory.getBean("schuelerService");
Schueler mustermann = new Schueler();
mustermann.setName("mustermann");
mustermann.setVorname("vorname");
mustermann = schuelerService.createUser(mustermann);
}
As you see, I am using hibernate + spring, and here is my spring configuration, if needed:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
.........
<tx:annotation-driven/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/db"/>
<property name="username" value="root"/>
<property name="password" value=",,,,,"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="hibernateProperties">
<value>
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
<property name="annotatedClasses">
<list>
<value>domain.Schueler</value>
</list>
</property>
</bean>
<bean id="schuelerDao" class="dao.impl.SchuelerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="schuelerService" class="service.impl.SchuelerServiceImpl">
<property name="schuelerDao" ref="schuelerDao" />
</bean>
</beans>
I read that hibernate objects are persistent, when loaded from the DB for example, and that they are automatically updated, so I tried in my service layer:
Code:
@Override
@Transactional
public Schueler createUser(Schueler schueler) {
Schueler s = schuelerDao.persistOrMerge(schueler);
s.setName("neuer name");
return s;
}
And yes, it saves the entry with the new name, "neuer name". So seems to work.
But when I try the same thing again, in the main(String[] args) method, using this SAME OBJECT, it doesn't update the database:
Code:
public static void main(String[] args) {
ApplicationContext beanFactory = new FileSystemXmlApplicationContext("./src/resources/applicationContext.xml");
SchuelerService schuelerService = (SchuelerService)beanFactory.getBean("schuelerService");
Schueler mustermann = new Schueler();
mustermann.setName("mustermann");
mustermann.setVorname("vorname");
mustermann = schuelerService.createUser(mustermann);
mustermann.setName("new name 2");
}
Why? It is using the same object used in createUser(Schueler schueler), and there it works.. why doesn't that work here?
Thanks!
(The persistent object is domain.Schueler)