Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's wrong in this Hibernate/Spring application (standalone)?
    primarykey
    data
    text
    <p>I have searched Google for the standalone Hibernate/Spring applications, but didn't find any useful sample. It seems like most people use it for web apps only. </p> <p>Here is what I have:</p> <p>The main class:</p> <pre><code>@Component public class App { @Inject SessionFactory sessionFactory; Fruit apple; Serializable appleId; @Transactional void testCreate() { apple = new Fruit(); apple.setName("Apple"); apple.setPrice(10); HibernateTemplate template = new HibernateTemplate(sessionFactory); appleId = template.save(apple); System.out.println("New Apple: " + apple); } @Transactional void testReload() { HibernateTemplate template = new HibernateTemplate(sessionFactory); final Fruit reload = template.load(Fruit.class, appleId); Session session = SessionFactoryUtils.getSession(sessionFactory, true); System.out.println("Update"); session.update(reload); System.out.println("Reload: " + reload); } public void run() throws Exception { testCreate(); testReload(); } public static void main(String[] args) throws Exception { new ClassPathXmlApplicationContext("context.xml").getBean(App.class).run(); } } </code></pre> <p>In this example, after successfully inserted a new <code>Apple</code> to the database, the subsequence reload() function thrown:</p> <p>The output:</p> <pre><code>Hibernate: /* insert my.hibernate.Fruit */ insert into Food (id, rank, version, name, price, DTYPE) values (null, ?, ?, ?, ?, 'Fruit') DEBUG [main] (HibernateAccessor.java:389) - Eagerly flushing Hibernate session New Apple: 1, Apple DEBUG [main] (HibernateAccessor.java:389) - Eagerly flushing Hibernate session Update Hibernate: /* load my.hibernate.Fruit */ select fruit0_.id as id0_0_, fruit0_.rank as rank0_0_, fruit0_.version as version0_0_, fruit0_.name as name0_0_, fruit0_.price as price0_0_ from Food fruit0_ where fruit0_.id=? and fruit0_.DTYPE='Fruit' Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [my.hibernate.Fruit#1] at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:419) at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:154) at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:143) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:174) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190) at my.hibernate.Fruit_$$_javassist_0.toString(Fruit_$$_javassist_0.java) at java.lang.String.valueOf(String.java:2902) at java.lang.StringBuilder.append(StringBuilder.java:128) at my.hibernate.App.testReload(App.java:86) </code></pre> <p>It seems like <code>testCreate()</code> didn't commit anything. Any idea?</p> <p><strong>EDIT</strong></p> <p>The <code>context.xml</code>:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "&gt; &lt;context:component-scan base-package="my" /&gt; &lt;/beans&gt; </code></pre> <p>And the session-factory config:</p> <pre><code>@Component public class TestH2DataSource extends BasicDataSource { public TestH2DataSource() { setDriverClassName("org.h2.Driver"); setUrl("jdbc:h2:target/testdb;DB_CLOSE_ON_EXIT=FALSE"); setUsername("sa"); setPassword(""); setDefaultAutoCommit(false); } } @Component public class TestSessionFactory extends AnnotationSessionFactoryBean { @Inject DataSource dataSource; public TestSessionFactory() { Properties properties = new Properties(); properties.setProperty("hibernate.show_sql", "true"); properties.setProperty("hibernate.format_sql", "true"); properties.setProperty("hibernate.use_sql_comments", "true"); properties.setProperty("hibernate.hbm2ddl.auto", "create-drop"); this.setHibernateProperties(properties); this.setAnnotatedClasses(new Class&lt;?&gt;[] { Fruit.class }); } @Override public void afterPropertiesSet() throws Exception { this.setDataSource(dataSource); super.afterPropertiesSet(); } } @Configuration public class OtherContextConfiguration { @Inject SessionFactory sessionFactory; @Bean public HibernateInterceptor hibernateInterceptor() { HibernateInterceptor hibernateInterceptor = new HibernateInterceptor(); hibernateInterceptor.setSessionFactory(sessionFactory); return hibernateInterceptor; } @Bean public HibernateTransactionManager hibernateTransactionManager() { HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager(); hibernateTransactionManager.setSessionFactory(sessionFactory); return hibernateTransactionManager; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload