2016-05-17 7 views
1

mysqlクラスタ用のHibernate設定(hibernate.cfg.xml)ファイルが必要です。mysqlcluster用のハイバネート設定ファイルを作成するには?

[Hibernate]自動POJOクラスと* .hbm.xmlファイルを生成します。

以下の設定を使用してMySQLデータベースにアクセスできます。

また、簡単なJDBC接続を使用してMYSQL NDB Clusterデータベースにアクセスすることもできます。

私が使用しているときに問題が発生しました。MYSQL NDB Cluster Hibernateを使用してデータベースにアクセスすることができませんでした。

接続MYSQL NDBクラスターのデータベースは、Hibernate構成ファイル(hibernate.cfg.xml)を使用して他の構成を提案してください。

私は解散が新しい方言であると考えますMySQL NDBクラスタテーブルタイプ。設定ファイルの そうでない場合は変更

<property name="hibernate.bytecode.use_reflection_optimizer">false</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.password">[email protected]!f!c!aldb</property> 
    <property name="hibernate.connection.pool_size">10</property> 
    <property name="hibernate.connection.url">jdbc:mysql://192.168.1.187:3306/haze_videocon_v0.8</property> 
    <property name="hibernate.connection.username">haze</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.search.autoregister_listeners">false</property> 
    <property name="hibernate.show_sql">true</property> 
    <property name="hibernate.validator.apply_to_ddl">false</property> 
</session-factory> 

答えて

0

起動しているMySQL Clusterのを持っていることが必要です。簡単にするために、クラスタを構成するすべてのノード(プロセス)は、アプリケーションとともに、同じ物理ホスト上で実行されます。

これらが使用されているMySQL Clusterの設定ファイルです:

のconfig.ini:

[ndbd default]noofreplicas=2 
datadir=/home/billy/mysql/my_cluster/data 

[ndbd] 
hostname=localhost 
id=3 

[ndbd] 
hostname=localhost 
id=4 

[ndb_mgmd] 
id = 1 
hostname=localhost 
datadir=/home/billy/mysql/my_cluster/data 

[mysqld] 
hostname=localhost 
id=101 

[api] 
hostname=localhost 

のmy.cnf:

[mysqld] 
ndbcluster 
datadir=/home/billy/mysql/my_cluster/data 
basedir=/usr/local/mysql 

これはClusterJに焦点を当てではなく、 MySQL Clusterを実行している場合MySQL Clusterを初めてお使いの方は、これを試す前に単純なクラスタを実行することを参照してください。

ClusterJには、MySQL Clusterデータベースへの接続方法が記載されている必要があります。接続文字列(管理ノードのアドレス/ポート)、使用するデータベース、ログインするユーザー、接続の属性(タイムアウト値など)これらのパラメータが定義されていないと、ClusterJは実行時例外で失敗します。この情報は、図3に示す「構成プロパティー」を表しています。これらのパラメーターは、アプリケーション・コード内でハードコーディングできますが、アプリケーションによってインポートされるclusterj.propertiesファイルを作成する方がメンテナンスが容易です。このファイルは、アプリケーションのソースコードと同じディレクトリに格納する必要があります。

clusterj.properties:

com.mysql.clusterj.connectstring=localhost:1186 
com.mysql.clusterj.database=clusterdb 
com.mysql.clusterj.connect.retries=4 
com.mysql.clusterj.connect.delay=5 
com.mysql.clusterj.connect.verbose=1 
com.mysql.clusterj.connect.timeout.before=30 
com.mysql.clusterj.connect.timeout.after=20 
com.mysql.clusterj.max.transactions=1024 

ClusterJが自動的にテーブルを作成しないように、次のステップはclusterjにいう「clusterdbでは」データベースを(作成することです。特性)および '従業員' テーブル:

Employee.java:

import com.mysql.clusterj.annotation.Column; 
import com.mysql.clusterj.annotation.Index; 
import com.mysql.clusterj.annotation.PersistenceCapable; 
import com.mysql.clusterj.annotation.PrimaryKey; 
@PersistenceCapable(table="employee") 
@Index(name="idx_uhash") 
public interface Employee { 
@PrimaryKey 
int getId(); 
void setId(int id); 
String getFirst(); 
void setFirst(String first); 

String getLast(); 
void setLast(String last); 
@Column(name="municipality") 
@Index(name="idx_municipality") 
String getCity(); 
void setCity(String city); 
String getStarted(); 
void setStarted(String date); 
String getEnded(); 
void setEnded(String date); 
Integer getDepartment(); 
void setDepartment(Integer department); 
} 

テーブルの名前を

[[email protected] ~]$ mysql -u root -h 127.0.0.1 -P 3306 -u root 
mysql> create database clusterdb;use clusterdb; 
mysql> CREATE TABLE employee (
->  id INT NOT NULL PRIMARY KEY, 
->  first VARCHAR(64) DEFAULT NULL, 
->  last VARCHAR(64) DEFAULT NULL, 
->  municipality VARCHAR(64) DEFAULT NULL, 
->  started VARCHAR(64) DEFAULT NULL, 
->  ended VARCHAR(64) DEFAULT NULL, 
->  department INT NOT NULL DEFAULT 1, 
->  UNIQUE KEY idx_u_hash (first,last) USING HASH, 
->  KEY idx_municipality (municipality) 
->) ENGINE=NDBCLUSTER; 

次のステップは、注釈付きインタフェースを作成することです@PersistenceCapable(table = "employee")の注釈で指定され、従業員テーブルの各列には、インタフェースで定義されている関連getterおよびsetterメソッドがあります。既定では、インターフェイスのプロパティ名はテーブルの列名と同じです。関連するgetterメソッドの直前に@Column(name = "municipality")注釈を明示的に組み込むことによって、Cityプロパティの列名がオーバーライドされました。 @PrimaryKey注釈は、関連する列が表の主キーであるプロパティを識別するために使用されます。 ClusterJは、@Indexアノテーションを使用してデータベースにインデックスが存在することを認識します。

次のステップは、ブロックごとにここでステップするアプリケーションコードを書き込むことです。最初単にインポート文を含み、次いで上記で定義clusterj.propertiesの内容をロードするの:

Main.java(パート1):

import com.mysql.clusterj.ClusterJHelper; 
import com.mysql.clusterj.SessionFactory; 
import com.mysql.clusterj.Session; 
import com.mysql.clusterj.Query; 
import com.mysql.clusterj.query.QueryBuilder; 
import com.mysql.clusterj.query.QueryDomainType; 
import java.io.File; 
import java.io.InputStream; 
import java.io.FileInputStream; 
import java.io.*; 
import java.util.Properties; 
import java.util.List; 
public class Main { 
public static void main (String[] args) throws java.io.FileNotFoundException,java.io.IOException { 
// Load the properties from the clusterj.properties file 
File propsFile = new File("clusterj.properties"); 
InputStream inStream = new FileInputStream(propsFile); 
Properties props = new Properties(); 
props.load(inStream); 
//Used later to get userinput 
BufferedReader br = new BufferedReader(new 
InputStreamReader(System.in)); 

次のステップを取得しますclusterj.propertiesファイルからインポートしたプロパティに基づいて(ClusterJHelperクラスからのSessionFactoryのために処理し、セッションを作成するために、そのファクトリを使用

Main.java(パート2):。

//セッションの作成(データベースへの接続) SessionFactory factory = ClusterJHelper.getSessionFactory(props); セッションセッション= factory.getSession(); セッションが終了したので、新しいEmployeeオブジェクトをインスタンス化してデータベースに保持することができます。トランザクションbegin()またはcommit()ステートメントがない場合、データベースを含む各操作は別々のトランザクションとして扱われます。

Main.java(その3):この時点で

/

/ Create and initialise an Employee 
Employee newEmployee = session.newInstance(Employee.class); 
newEmployee.setId(988); 
newEmployee.setFirst("John"); 
newEmployee.setLast("Jones"); 
newEmployee.setStarted("1 February 2009"); 
newEmployee.setDepartment(666); 
// Write the Employee to the database 
session.persist(newEmployee); 

、行は、「従業員」テーブルに追加されているであろう。これを確認するには、新しい従業員オブジェクトが作成され、主キー(ID)を用いて、バック '従業員テーブルからデータを読み取るために使用される998の値:

Main.java(その4):

// Fetch the Employee from the database 
Employee theEmployee = session.find(Employee.class, 988); 
if (theEmployee == null) 
{System.out.println("Could not find employee");} 
else 
{System.out.println ("ID: " + theEmployee.getId() + "; Name: " + 
theEmployee.getFirst() + " " + theEmployee.getLast()); 
System.out.println ("Location: " + theEmployee.getCity()); 
System.out.println ("Department: " + theEmployee.getDepartment()); 
System.out.println ("Started: " + theEmployee.getStarted()); 
System.out.println ("Left: " + theEmployee.getEnded()); 
} 

これは、この時点で見出力されます:私は、従業員変更する前に

ID: 988; Name: John Jones 
Location: null 
Department: 666 
Started: 1 February 2009 
Left: null 

は、データベースをチェックしてください - ヒット復帰を完了したら 次のステップでは、このデータを変更することですが、それは書き込みません。それに戻るデータベースはまだ:

メイン。JAVA(その5):

// Make some changes to the Employee & write back to the database 
theEmployee.setDepartment(777); 
theEmployee.setCity("London"); 
System.out.println("Check the database before I change the Employee - 
hit return when you are done"); 
String ignore = br.readLine(); 

アプリケーションは、この時点で一時停止し、元のデータが新しい行として追加されましたが、変更が書き戻されていないことを確認するためにデータベースをチェックするためにあなたのチャンスを与えますまだ:

mysql> select * from clusterdb.employee; 
+-----+-------+-------+--------------+-----------------+-------+------------+ 
| id | first | last | municipality | started   | ended | department | 
+-----+-------+-------+--------------+-----------------+-------+------------+ 
| 988 | John | Jones | NULL   | 1 February 2009 | NULL |  666 | 
+-----+-------+-------+--------------+-----------------+-------+------------+ 
リターンした後、アプリケーションは更新を実行するために自動トランザクションを使用してテーブルに変更を書き続けます。

Main.java(その6):

session.updatePersistent(theEmployee); 
System.out.println("Check the change in the table before I bulk add 
Employees - hit return when you are done"); 
ignore = br.readLine(); 

アプリケーションは再び我々は今、変更がデータベースに(永続化)バック書き込まれていることを確認することができるように、一時停止します:

mysql> select * from clusterdb.employee; 
+-----+-------+-------+--------------+-----------------+-------+------------+ 
| id | first | last | municipality | started   | ended | department | 
+-----+-------+-------+--------------+-----------------+-------+------------+ 
| 988 | John | Jones | London  | 1 February 2009 | NULL |  777 | 
+-----+-------+-------+--------------+-----------------+-------+------------+ 

アプリケーションは、100人の新規従業員を作成し、永続化します。

Main.java(その7):

// Add 100 new Employees - all as part of a single transaction 
newEmployee.setFirst("Billy"); 
newEmployee.setStarted("28 February 2009"); 
session.currentTransaction().begin(); 
for (int i=700;i<800;i++) { 
newEmployee.setLast("No-Mates"+i); 
newEmployee.setId(i+1000); 
newEmployee.setDepartment(i); 
session.persist(newEmployee); 
} 
session.currentTransaction().commit(); 
をパフォーマンスを向上させるために、単一のトランザクションがコミット()文が実行されたときにすべての変更が一度にデータベースに書き込むことができるように使用されています

100人の新規従業員がデータベースに永続化されました。次のステップは、QueryBuilderを使用して部門777のすべての従業員のデータベースを検索し、それを使用して 'department'列とパラメーターを比較するQueryDomainを構築する照会を作成して実行することです。を作成した後、departmentパラメーターは777に設定されます(照会はその後、異なる部門番号で再使用される可能性があります)。アプリケーションは、次にを通じてクエリと反復処理を実行し、結果セット内の従業員のそれぞれを表示:

Main.java(その8):

// Retrieve the set all of Employees in department 777 
QueryBuilder builder = session.getQueryBuilder(); 
QueryDomainType<Employee> domain = 
builder.createQueryDefinition(Employee.class); 
domain.where(domain.get("department").equal(domain.param(
"department"))); 
Query<Employee> query = session.createQuery(domain); 
query.setParameter("department",777); 
List<Employee> results = query.getResultList(); 
for (Employee deptEmployee: results) { 
System.out.println ("ID: " + deptEmployee.getId() + "; Name: " + 
deptEmployee.getFirst() + " " + deptEmployee.getLast()); 
System.out.println ("Location: " + deptEmployee.getCity()); 
System.out.println ("Department: " + deptEmployee.getDepartment()); 
System.out.println ("Started: " + deptEmployee.getStarted()); 
System.out.println ("Left: " + deptEmployee.getEnded()); 
} 
System.out.println("Last chance to check database before emptying table 
- hit return when you are done"); 
ignore = br.readLine(); 

この時点で、アプリケーションは次のように表示されそしてそれが継続できるようにするようにユーザに促す:

ID: 988; Name: John Jones 
Location: London 
Department: 777 
Started: 1 February 2009 
Left: null 
ID: 1777; Name: Billy No-Mates777 
Location: null 
Department: 777 
Started: 28 February 2009 
Left: null 

私たちは、データベース上で実行されたSQLクエリとその出力を比較することができます

Main.java(その9):最終チェックとして

session.deletePersistentAll(Employee.class); 

、SQLクエリがあることを確認し

mysql> select * from employee where department=777; 
+------+-------+-------------+--------------+------------------+-------+------------+ 
| id | first | last  | municipality | started   | ended | department | 
+------+-------+-------------+--------------+------------------+-------+------------+ 
| 988 | John | Jones  | London  | 1 February 2009 | NULL |  777 | 
| 1777 | Billy | No-Mates777 | NULL   | 28 February 2009 | NULL |  777 | 
+------+-------+-------------+--------------+------------------+-------+------------+ 

最後に、再びリターンを押した後、アプリケーションは、すべての従業員を削除しますすべての行が「従業員」テーブルから削除されました。

mysql> select * from employee; 
Empty set (0.00 sec) 
関連する問題