-->
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: Problem writing simple hello world program in hibernate
PostPosted: Fri Jul 17, 2009 4:00 am 
Newbie

Joined: Wed Jul 15, 2009 4:27 am
Posts: 4
I am using maven2 to write a simple hello world program
I have three files in src/main/java/hello folder
HelloWorld.java
Message.java
Message.hbm.xml
I have one file in src/main/java/persistence
HibernateUtil.java


source is as follows
#HelloWorld.java
Code:
package hello;

import org.hibernate.*;
import persistence.*;
import java.util.*;

public class HelloWorld {

    public static void main(String[] args) {

        // ############################################################################

        // First unit of work
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();

        Message message = new Message("Hello World");
        session.save(message);

        tx.commit();
        session.close();

        // ############################################################################

        // Second unit of work
        Session secondSession = HibernateUtil.getSessionFactory().openSession();
        Transaction secondTransaction = secondSession.beginTransaction();

        List messages =
            secondSession.createQuery("from Message m order by m.text asc").list();

        System.out.println( messages.size() + " message(s) found:" );

        for ( Iterator iter = messages.iterator(); iter.hasNext(); ) {
            Message loadedMsg = (Message) iter.next();
            System.out.println( loadedMsg.getText() );
        }

        secondTransaction.commit();
        secondSession.close();

        // ############################################################################

        // Third unit of work
        Session thirdSession = HibernateUtil.getSessionFactory().openSession();
        Transaction thirdTransaction = thirdSession.beginTransaction();

        // message.getId() holds the identifier value of the first message
        Message loadedMessage = (Message) thirdSession.get( Message.class, message.getId());
        loadedMessage.setText( "Greetings Earthling" );
        loadedMessage.setNextMessage(
            new Message( "Take me to your leader (please)" )
        );

        thirdTransaction.commit();
        thirdSession.close();

        // ############################################################################

        // Final unit of work (just repeat the query)
        // TODO: You can move this query into the thirdSession before the commit, makes more sense!
        Session fourthSession = HibernateUtil.getSessionFactory().openSession();
        Transaction fourthTransaction = fourthSession.beginTransaction();

        messages =
            fourthSession.createQuery("from Message m order by m.text asc").list();

        System.out.println( messages.size() + " message(s) found:" );

        for ( Iterator iter = messages.iterator(); iter.hasNext(); ) {
            Message loadedMsg = (Message) iter.next();
            System.out.println( loadedMsg.getText() );
        }

        fourthTransaction.commit();
        fourthSession.close();


        // Shutting down the application
        HibernateUtil.shutdown();
    }
}

#Message.java
Code:
package hello;

public class Message {
    private Long id;
    private String text;
    private Message nextMessage;

    Message() {}

    public Message(String text) {
        this.text = text;
    }

    public Long getId() {
        return id;
    }
    private void setId(Long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }

     public Message getNextMessage() {
         return nextMessage;
     }
     public void setNextMessage(Message nextMessage) {
         this.nextMessage = nextMessage;
     }
}

#Message.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class
        name="hello.Message"
        table="MESSAGES">

        <id
            name="id"
            column="MESSAGE_ID">
            <generator class="native"/>
        </id>

        <property
            name="text"
            column="MESSAGE_TEXT"/>

        <many-to-one
            name="nextMessage"
            cascade="all"
            column="NEXT_MESSAGE_ID"
            foreign-key="FK_NEXT_MESSAGE"/>

    </class>

</hibernate-mapping>

I have #hibernate.cfg.xml in resources folder is as follows
Code:
<?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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</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 resource="hello/Message.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

I am getting output as
Code:
C:\Users\mypc\hibernate\2>mvn -e exec:java -Dexec.mainClass="hello.HelloWorld"
+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] ------------------------------------------------------------------------
[INFO] Building 2
[INFO]    task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
Downloading: http://repo1.maven.org/maven2/asm/asm/1.0/asm-1.0.pom
[INFO] Unable to find resource 'asm:asm:pom:1.0' in repository central (http://repo1.maven.
org/maven2)
Downloading: http://repo1.maven.org/maven2/stax/stax/1.0/stax-1.0.pom
[INFO] Unable to find resource 'stax:stax:pom:1.0' in repository central (http://repo1.mave
n.org/maven2)
Downloading: http://repo1.maven.org/maven2/stax/stax-ri/1.0/stax-ri-1.0.pom
[INFO] Unable to find resource 'stax:stax-ri:pom:1.0' in repository central (http://repo1.m
aven.org/maven2)
Downloading: http://repo1.maven.org/maven2/clover/clover/1.3-rc4/clover-1.3-rc4.pom
[INFO] Unable to find resource 'clover:clover:pom:1.3-rc4' in repository central (http://re
po1.maven.org/maven2)
Downloading: http://repo1.maven.org/maven2/org/hibernate/hibernate3/3.3.2.GA/hibernate3-3.3
.2.GA.pom
[INFO] Unable to find resource 'org.hibernate:hibernate3:pom:3.3.2.GA' in repository centra
l (http://repo1.maven.org/maven2)
[INFO] [exec:java {execution: default-cli}]
Jul 17, 2009 1:16:55 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.6
Jul 17, 2009 1:16:55 PM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Jul 17, 2009 1:16:56 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Jul 17, 2009 1:16:56 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Jul 17, 2009 1:16:56 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Jul 17, 2009 1:16:56 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Jul 17, 2009 1:16:57 PM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : hello/Message.hbm.xml
Jul 17, 2009 1:16:57 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: hello.Message -> MESSAGES
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Jul 17, 2009 1:16:58 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
Jul 17, 2009 1:16:58 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 1
Jul 17, 2009 1:16:58 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
Jul 17, 2009 1:16:58 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://localhost
Jul 17, 2009 1:16:58 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=sa, password=****}
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: HSQL Database Engine, version: 1.8.0
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: HSQL Database Engine Driver, version: 1.8.0
Jul 17, 2009 1:16:58 PM org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.HSQLDialect
Jul 17, 2009 1:16:58 PM org.hibernate.transaction.TransactionFactoryFactory buildTransactio
nFactory
INFO: Using default transaction strategy (direct JDBC transactions)
Jul 17, 2009 1:16:58 PM org.hibernate.transaction.TransactionManagerLookupFactory getTransa
ctionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or tran
sactional second-level cache is not recommended)
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): disabled
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
Jul 17, 2009 1:16:58 PM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
Jul 17, 2009 1:16:59 PM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
Jul 17, 2009 1:16:59 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
Jul 17, 2009 1:16:59 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
Jul 17, 2009 1:16:59 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
Jul 17, 2009 1:16:59 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
Jul 17, 2009 1:16:59 PM org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
Jul 17, 2009 1:16:59 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
Jul 17, 2009 1:16:59 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
Jul 17, 2009 1:16:59 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
Jul 17, 2009 1:16:59 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
Jul 17, 2009 1:16:59 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
Jul 17, 2009 1:16:59 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
Jul 17, 2009 1:16:59 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
Jul 17, 2009 1:16:59 PM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. null

org.objectweb.asm.ClassWriter.<init>(I)V
[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: An exception occured while executin
g the Java class. null
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycl
eExecutor.java:703)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(Defaul
tLifecycleExecutor.java:553)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycle
Executor.java:523)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures
(DefaultLifecycleExecutor.java:371)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultL
ifecycleExecutor.java:332)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExec
utor.java:181)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:356)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:137)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:41)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while execu
ting the Java class. null
        at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:338)
        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.ja
va:483)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycl
eExecutor.java:678)
        ... 17 more
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:283)
        at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ExceptionInInitializerError
        at persistence.HibernateUtil.<clinit>(HibernateUtil.java:17)
        at hello.HelloWorld.main(HelloWorld.java:14)
        ... 6 more
Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
        at net.sf.cglib.core.DebuggingClassWriter.<init>(DebuggingClassWriter.java:47)
        at net.sf.cglib.core.DefaultGeneratorStrategy.getClassWriter(DefaultGeneratorStrate
gy.java:30)
        at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.jav
a:24)
        at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)

        at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144)
        at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:116)
        at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
        at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
        at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
        at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyIni
tializer.java:117)
        at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.postInstantiate(CGLIBProxyFacto
ry.java:43)
        at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTupliz
er.java:162)
        at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.
java:135)
        at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55)

        at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntity
ModeToTuplizerMapping.java:56)
        at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:302)
        at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPers
ister.java:434)
        at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEnti
tyPersister.java:109)
        at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.j
ava:55)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
        at persistence.HibernateUtil.<clinit>(HibernateUtil.java:15)
        ... 7 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17 seconds
[INFO] Finished at: Fri Jul 17 13:17:00 IST 2009
[INFO] Final Memory: 7M/12M
[INFO] ------------------------------------------------------------------------
Jul 17, 2009 1:17:00 PM org.hibernate.connection.DriverManagerConnectionProvider close
INFO: cleaning up connection pool: jdbc:hsqldb:hsql://localhost

I have wasted two days in finding what the problem is . Somebody please help me figuring out what the problem is


Top
 Profile  
 
 Post subject: Re: Problem writing simple hello world program in hibernate
PostPosted: Fri Jul 17, 2009 7:03 am 
Beginner
Beginner

Joined: Wed Jun 17, 2009 9:03 pm
Posts: 31
Location: mumbai
I guess you have wrong version of asm.jar and asm-attrs.jar in your classpath.


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.