2016-05-17 63 views
1

需要针对mysql集群的Hibernate配置(hibernate.cfg.xml)文件。如何为mysqlcluster创建hibernate配置文件?

[Hibernate] Auto生成POJO类和* .hbm.xml文件。

我可以使用以下配置访问Mysql数据库。

而且我还能够使用简单的JDBC连接访问MYSQL NDB Cluster数据库。

问题是当我使用MYSQL NDB群集数据库凭证,当时我无法使用Hibernate访问数据库。

请建议使用Hibernate配置文件(hibernate.cfg.xml)的数据库的连接的任何其他配置MYSQL NDB CLuster

我认为解决方案是新的方言需要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集群启动和运行。为了简单起见,组成群集的所有节点(进程)将与应用程序一起运行在同一台物理主机上。

这些都是正在使用的MySQL集群配置文件:

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集群;如果您是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不会自动创建表,接下来的步骤是创建“clusterdb”数据库(在clusterj提及。性)和 '雇员' 表:

[[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; 

下一步是创建注释的接口:

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); 
} 

的表的名称在注解@PersistenceCapable(table =“employee”)中指定,然后员工表中的每列都有一个在接口中定义的关联getter和setter方法。默认情况下,接口中的属性名称与表中的列名称相同 - 通过在关联的getter方法之前明确包含@Column(name =“municipality”)注释,City属性的列名已被覆盖。 @PrimaryKey注释用于标识其关联列是表中主键的属性。使用@Index注释使ClusterJ意识到数据库中索引的存在。

下一步是编写我们在这里逐块执行的应用程序代码;其仅包含导入语句,然后第一加载上面所定义的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)); 

下一步是得到一个。办理从ClusterJHelper类一个SessionFactory,然后使用该工厂创建一个会话(基于clusterj.properties文件导入属性

Main.java(部分2):

//创建会话(连接到数据库) SessionFactory factory = ClusterJHelper.getSessionFactory(props); Session session = 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); 

此时,一个行将已被添加到“雇员”表。为了验证这一点,新的Employee对象被创建并用于使用主键998(Id)的值,以读出的数据从“雇员”表背:

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 

检查数据库之前,我改变了员工 - 当您完成 下一步就是修改这个数据,但它并没有写回车它回到数据库还没有:

主。爪哇(第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中搜索数据库中的所有员工,并使用该查询构建将“部门”列与参数进行比较的QueryDomain。创建后,部门参数设置为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查询:

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 | 
+------+-------+-------------+--------------+------------------+-------+------------+ 

最后,再次按下返回后,应用程序会删除所有员工:

Main.java(第9部分):

session.deletePersistentAll(Employee.class); 

最后检查,SQL查询确认所有的行都已从'员工'表中删除。

mysql> select * from employee; 
Empty set (0.00 sec)