-->
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: How to connect my new schema using Eclipse
PostPosted: Tue Jul 29, 2008 11:07 am 
Newbie

Joined: Tue Jul 29, 2008 10:51 am
Posts: 1
Location: Singapore
Hi Im using Eclipse 3.x version.I have created a new schema and also specified the path in hibernate.cfg.xml file.How to create Entity java files using this IDE version.?

when i compiled i couldnt see any new Entity files .Pls help me..Im new for Hibernate.

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 7:52 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, make sure your schema is created, and then, to create tables use a SchemaExport:

Code:
  public static void main(String args[]) {
    AnnotationConfiguration config =
                 new AnnotationConfiguration();
    config.addAnnotatedClass(User.class);
    config.configure();
    new SchemaExport(config).create(true, true);
  }


http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=01howtogetstartedwithhibernate

Image

That will create the tables you need. Then, just code some CRUD methods!

Code:
    config.addAnnotatedClass(User.class);
    config.configure();
    // new SchemaExport(config).create(true, true);
    SessionFactory factory =
                  config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();
    System.out.println("creating user");
   
    User u = new User();
    u.setPassword("abc123");
    session.saveOrUpdate(u);
    System.out.println("user saved");
    session.getTransaction().commit();
    System.out.println("transaction successful!!!"); 


But of course, make sure your hibernate.cfg.xml file has all the proper dialects for your database, connection URLs, usernames and passwords:

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>
  <property name="connection.url">
  jdbc:mysql://localhost/examscam
  </property>

  <property name="connection.username">
  root
  </property>
  <property name="connection.password">
  password
  </property>

  <property name="connection.driver_class">
  com.mysql.jdbc.Driver
  </property>

  <property name="dialect">
  org.hibernate.dialect.MySQLDialect
  </property>

  <property name="transaction.factory_class">
  org.hibernate.transaction.JDBCTransactionFactory
  </property>

  <property name="current_session_context_class">
  thread
  </property>

  <!-- this will show us all sql statements -->
  <property name="hibernate.show_sql">
  true
  </property>
  <!-- mapping files -->
  </session-factory>

</hibernate-configuration>


Check out this tutorial on setting up Hibernate:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=01howtogetstartedwithhibernate

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.