-->
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.  [ 6 posts ] 
Author Message
 Post subject: creating tables with hibernate
PostPosted: Fri Feb 19, 2010 10:54 am 
Newbie

Joined: Wed Oct 14, 2009 5:16 am
Posts: 5
Hi.

My question is - Is it possible to configure hibernate so that it creates a table when I try to save a class which is mapped to non existing table?
If it's possible then how?

My configuration file looks like this so far:

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 name="session">
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:jtds:sqlserver://PLKRK-W-CRC0190:1279/HibernateDB</property>
        <property name="hibernate.connection.username">testuser</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.connection.password">pass</property>
        <property name="hibernate.hbm2ddl.auto">create-drop</property>
    </session-factory>
</hibernate-configuration>


main goal is to make this code to work:

Code:
Example e1 = new Example();
//filling field of e1
Example e2 = new Example();
//filling field of e2
      
Session session = factory.openSession();

Transaction tx = session.beginTransaction();
session.save(e1);
session.save(e2);
tx.commit();
      
session.close();


stack trace when I try to run the test:
Code:
org.hibernate.MappingException: Unknown entity: hibernate.test.Example1
   at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:580)
   at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1365)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
   at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
   at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
   at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)
   at hibernate.test.HibernateTest.createDB(HibernateTest.java:29)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
   at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
   at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
   at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
   at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
   at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
   at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
   at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
   at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
   at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
   at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
   at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
   at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
   at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


Top
 Profile  
 
 Post subject: Re: creating tables with hibernate
PostPosted: Mon Feb 22, 2010 5:45 am 
Newbie

Joined: Wed Oct 14, 2009 5:16 am
Posts: 5
I don't want to sound rude or desperate, but pls help... Anyone?


Top
 Profile  
 
 Post subject: Re: creating tables with hibernate
PostPosted: Mon Feb 22, 2010 7:05 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
Quote:
<property name="hibernate.hbm2ddl.auto">create-drop</property>


The above option tells hibernate to create table.
However you have to create a mapping file(Example.hbm.xml) and hibernate will use that file to create the table.

And also don't forget to include the mapping file name in hibernate.cfg.xml

The following tutorial does exactly that with the in memory hsql database
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/tutorial.html


Top
 Profile  
 
 Post subject: Re: creating tables with hibernate
PostPosted: Mon Feb 22, 2010 8:25 am 
Newbie

Joined: Wed Oct 14, 2009 5:16 am
Posts: 5
thanks a lot for answer... I'll check on that tutorial. I was afraid that I will need another configuration file (mapping file in this case).

Originally I was aiming to do as little xml's etc as I can (I thought that it's possible to configure hibernate through annotations only)... I guess I was wrong.


Top
 Profile  
 
 Post subject: Re: creating tables with hibernate
PostPosted: Mon Feb 22, 2010 8:31 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
I haven't used annotations. But if you are using annotations, you don't need the mapping file to generate tables.

I think you have to specify this in your session factory.

Code:
<mapping class="xx.xx.xx.ClassName"/>
.


Top
 Profile  
 
 Post subject: Re: creating tables with hibernate
PostPosted: Mon Feb 22, 2010 9:00 am 
Newbie

Joined: Wed Oct 14, 2009 5:16 am
Posts: 5
Gosh... our servers went down a minute before I checked your reply :( ahhh... never mind... I will check that later...

I assume you meant something like this:

Code:
@Before
   public void init() {
      Configuration cfg = new Configuration();
      cfg.addClass(Example1.class);
      cfg.buildMappings();
          factory = cfg.configure().buildSessionFactory();
   }


I will let you know what I found later ;)
P.S. thanks a lot for help. I really appreciate that


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.