2011-08-16 11 views
0

私はHibernateを初めて使っていて、最初の基本を質問したかっただけです。私のような私のHibernateのBeanを作成しましたhibernateメインメソッド呼び出し

...私はそこに私はhibernate.cfg.xmlとDBコネクションを設定した

package com.behaviour.chapter1; 

import javax.persistence.Entity; 
import javax.persistence.Id; 

@Entity 
public class User { 

    private int userId; 
    private String firstName; 

    @Id 
    public int getUserId() { 
     return userId; 
    } 
    public void setUserId(int userId) { 
     this.userId = userId; 
    } 
    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 
} 

。私の質問は単純に、実際にメインメソッドからこれを呼び出し、Hibernate3.6.6でどのように使用するのですか?私はチュートリアルをオンラインで行っていましたが、それはHibernate3.2用であり、それは少し違っているようです。もし誰かがこのbeanを呼び出すだけの本当にシンプルなメインメソッドを見せてくれたら、(このテーブルにユーザを作成する)新しいユーザを作成すると大いに感謝します。また、 - 誰もが偉大な:)

おかげで、

答えて

4

これにはいくつかの方法がありますが、それは設計上の選択、これはhibernate.cfg.xmlファイルからセッションファクトリを作成することになります達成するための基本的な方法の問題です。ファイルをクラスパスに配置できることを確認してください。

以下クラスを使用して、その後、開くために使用されるセッションファクトリオブジェクトを作成し、新規セッションの

public class HibernateUtil 
    { 
    private static final SessionFactory sessionFactory; 

    static 
    { 
     try 
     { 
     // Create the SessionFactory from hibernate.cfg.xml 
     sessionFactory = new Configuration().configure().buildSessionFactory(); 
     } 
     catch (Throwable ex) 
     { 
      // Make sure you log the exception, as it might be swallowed 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
     } 

     public static SessionFactory getSessionFactory() 
     { 
     return sessionFactory; 
     } 
    } 

新規ユーザーを作成するには、実行します。

public class DaoFactory 
{ 
    public void create(Object obj)throws Exception 
    { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    session.beginTransaction(); 
    session.save(obj); 
    session.getTransaction().commit(); 
    } 
} 

メイン

EDIT

あなたhibernate.cfg.xml次のようなものになります。

<?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">com.mysql.jdbc.Driver</property> 
<property name="connection.url">jdbc:mysql://localhost:3306/test</property> 
<property name="connection.username">root</property> 
<property name="connection.password">root</property> 

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

<!-- SQL dialect --> 
<property name="dialect">org.hibernate.dialect.MySQLDialect</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">none</property> 

<mapping class="com.behaviour.chapter1.User"/> 

</session-factory> 
</hibernate-configuration> 
+0

ああ、ありがとうございます:)私はちょうど尋ねることができます、私は使用できるテンプレートhibernate.cfg.xmlを持っていますか?私は上記を試みたが、私はほぼ100%がXMLファイルにプロパティまたは2が欠落していることを確認してエラーが発生しました。 – david99world

+0

編集された回答を参照してください。 – Bitmap

+0

どのようにしてHibernateを設定しましたか? –

0

だろうか良い休止チュートリアルのリンクを持っている場合は、アクセスにSessionオブジェクトが必要ですパーシスタンスユニット。これらのオブジェクトは、SessionFactoryオブジェクトによって提供されます。

チェックthe Hibernate reference guide

1

私は既にあなたのpersistence.xmlを設定していると仮定しています。その場合は、次のJavaコードを使用できます。 JDBCセットアップデータと永続性ユニットで "..."を置き換える必要があります。

private static final String PERSISTENCE_UNIT = "..."; 

final Map<String, String> properties = new HashMap<String, String>(); 
properties.put("javax.persistence.jdbc.driver", "..."); 
properties.put("javax.persistence.jdbc.url", "..."); 
properties.put("javax.persistence.jdbc.user", "..."); 
properties.put("javax.persistence.jdbc.password", "..."); 

final EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT, properties); 
final EntityManager em = emf.createEntityManager(); 

User user = new User(); 
user.setUserID(0); 
user.setFirstName("David"); 

em.getTransaction().begin(); 
em.persist(user); 
em.getTransaction().commit(); 

HTH

+0

ああ、私はセットアップのpersistence.xmlていませんでした、どうもありがとうございましたが、あなたは私にこの事例を教えてもらえますか?また、私はPERSISTANCE_UNITに何を入れるべきかまだ分かりません。あなたは例を挙げることができますか? – david99world

関連する問題