Results 1 to 3 of 3

Thread: Problem with Spring and Hibernate Annotations

  1. #1
    Join Date
    Jan 2009
    Posts
    2

    Default Problem with Spring and Hibernate Annotations

    Hi there,
    i m new to Spring, and also Hibernate Annotations -_-
    i m writing a very simple example to see how it interacts with the DB. But i m so frustrated because i couldn't get it work properly, if i take off the (at)Entity from the JavaBean class, it runs ok, but nothing done to the DB, if i add (at)Entity in, it crashes at some stage, no change to DB either. Here's my code, please help me, i've been trying this for 2 days already, so sad ...Here's my spring context XML

    Sorry i have to user (at) to replace all the 'at' symbol, otherwise it won't post.

    Code:
    ...
    ...
    <beans>
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/hibernate_tut" />
    		<property name="username" value="test" />
    		<property name="password" value="test" />
    	</bean>
    	<!-- Hibernate session factory -->
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    		<property name="configLocation">
    			<value>hibernate.cfg.xml</value>
    		</property>
    		<property name="configurationClass">
    			<value>org.hibernate.cfg.AnnotationConfiguration</value>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.MySQLInnoDBDialect
    				</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.hbm2ddl.auto">update</prop>
    			</props>
    		</property>
    	</bean>
    </beans>
    this is the Hibernate mapping file
    Code:
    ............
    	<hibernate-configuration>
    		<session-factory>
    			<mapping class="au.com.hibernatetest.model.Client"/>
    		</session-factory>
    	</hibernate-configuration>
    This is the JavaBean
    Code:
    package au.com.hibernatetest.model;
    
    import java.io.Serializable;
    import java.util.Date;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    (at)Entity   // --------------------------- without this, it runs, but no DB change, with this, crashes
    (at)Table(name="hb_client")
    public class Client implements Serializable {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 7827154723198058789L;
    
    	private Long pkId;
    
    	private Date lastUpdated;
    
    	private String username;
    
    	private String password;
    
    	(at)Id
    	(at)Column(name = "pk_client_id")
    	(at)GeneratedValue(strategy = GenerationType.AUTO)
    	public Long getPkId() {
    		return pkId;
    	}
    
    	public void setPkId(Long pkId) {
    		this.pkId = pkId;
    	}
    
    	public Date getLastUpdated() {
    		return lastUpdated;
    	}
    
    	public void setLastUpdated(Date lastUpdated) {
    		this.lastUpdated = lastUpdated;
    	}
    
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    }
    and the Test.java
    Code:
    package au.com.hibernatetest.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    	
    	private static ApplicationContext ctx = 
    		new ClassPathXmlApplicationContext(
    				new String[] {
    						"applicationContext.xml"
    				}); 
    	
    	public static void main(String args[]) {
    		try{
    			Test test = new Test();
    		}catch(Exception ex) {
    			ex.printStackTrace();
    		}
    	}
    }
    I use Maven2 to compile it, and run the Test.java. I suppose it should add a table into the DB when i run it.

    Thank you very much for the help, i'll be looking forward to your reply!!!!!

  2. #2
    Join Date
    Jan 2009
    Posts
    2

    Default sorry, forgot to post the error message

    Code:
    Exception in thread "main" java.lang.ExceptionInInitializerError
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.slf4j.impl.NOPLogger.trace(Ljava/lang/String;)V
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1088)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:429)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:250)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:247)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:161)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:270)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:346)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
    	at au.com.hibernatetest.test.Test.<clinit>(Test.java:9)
    Caused by: java.lang.AbstractMethodError: org.slf4j.impl.NOPLogger.trace(Ljava/lang/String;)V
    	at org.hibernate.cfg.annotations.PropertyBinder.make(PropertyBinder.java:184)
    	at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1911)
    	at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1279)
    	at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
    	at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
    	at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
    	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283)
    	at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:804)
    	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:744)
    	at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:131)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1118)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1085)
    	... 10 more
    Could not find the main class: au.com.hibernatetest.test.Test.  Program will exit.

  3. #3
    Join Date
    Aug 2008
    Location
    Phoenix, AZ
    Posts
    76

    Default

    I'm sorry you're experiencing such trouble. Unfortunately, you've posted in the wrong forum. This forum is for Spring Integration, which is Enterprise Application Integration, not spring-hibernate integration. Post future questions like this in the "Core Container" forum or "Data Access" forum.

    If you private message me, I'll take a look at your project and try to help you out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •