休眠和 MySQL 配置
Hibernate and MySQL configuration
我花了几个小时尝试设置我的第一个 Hibernate 应用程序,但它仍然无法运行。
我有 WAMP 服务器和我的 MySQL 数据库,名为 "hibernatetest"。我在 Eclipse 中有项目,其中包含 Hibernate 库和 mysql-connector-java-5.1.18-bin.jar。我也有这个 类:
HibernateUtil.java:
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory(
new StandardServiceRegistryBuilder().build() );
}
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;
}
}
test.java(包含主要内容):
import org.hibernate.Session;
import templates.Album;
public class test {
public static void main(String[] args){
Album i = new Album();
i.setID(1);
i.setArtist("Iron Maiden");
i.setTitle("The Book of Souls");
i.setLabel("Warner Music");
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(i);
session.getTransaction().commit();
session.close();
System.out.println("Saved");
}
}
Album.java:
package templates;
public class Album {
private int ID;
private String title;
private String artist;
private String label;
public Album(){
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
Album.hbm.xml: link
Hibernate.cfg.xml: link
堆栈跟踪:
wrz 15, 2015 10:04:47 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.1.Final}
wrz 15, 2015 10:04:47 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
wrz 15, 2015 10:04:47 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
wrz 15, 2015 10:04:47 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
wrz 15, 2015 10:04:48 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
wrz 15, 2015 10:04:49 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
wrz 15, 2015 10:04:49 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Exception in thread "main" java.lang.ExceptionInInitializerError
at HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at HibernateUtil.<clinit>(HibernateUtil.java:7)
at test.main(test.java:13)
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at HibernateUtil.buildSessionFactory(HibernateUtil.java:12)
... 2 more
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
... 15 more
我做错了什么?
如错误所述,您需要在 hibernate.cfg 文件中指定方言。
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
我注意到一些可能导致问题的原因:
1) 在您的 hibernate.cfg.xml
中,您没有指定连接密码。您可以通过添加 <property name="connection.password">your_Pass_here</property>
属性(或者出于隐私原因省略它?)
来实现
2) 正如@Prerak Tiwari 提到的,您没有指定方言。按照他提到的 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
属性
3) 我注意到您的属性名称与大多数人使用的名称不同。不要添加 ...name="hibernate.connection.driver_class"...
省略 hibernate.
部分,所以它应该如下所示:
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernatetest</property>
<property name="connection.username">root</property>
Your password property here...
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="/templates/Album.hbm.xml"/>
</session-factory>
希望其中一些对你有用:)
这就是我修复错误的方法。
我的环境和你的一样Hibernate 5+
, Mysql
& WAMP Server
- 确保包含
jta jar
,它包含在此位置 \hibernate-release-5.0.5.Final\lib\osgi
- 您应该添加新用户并授予所有权限。主机应该是
localhost
- 你的 hibernate.cfg.xml 应该是这样的
这就是它对我有用的方式。我想我必须给 localhost
而不是 %
我遇到了同样的问题。只需在 localhost 之后添加您的端口号。在我的例子中它是 localhost:3306
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_db</property>
检查密码是否正确 hibernate.cfg.xml
<property name="connection.password">root</property>
我花了几个小时尝试设置我的第一个 Hibernate 应用程序,但它仍然无法运行。
我有 WAMP 服务器和我的 MySQL 数据库,名为 "hibernatetest"。我在 Eclipse 中有项目,其中包含 Hibernate 库和 mysql-connector-java-5.1.18-bin.jar。我也有这个 类:
HibernateUtil.java:
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory(
new StandardServiceRegistryBuilder().build() );
}
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;
}
}
test.java(包含主要内容):
import org.hibernate.Session;
import templates.Album;
public class test {
public static void main(String[] args){
Album i = new Album();
i.setID(1);
i.setArtist("Iron Maiden");
i.setTitle("The Book of Souls");
i.setLabel("Warner Music");
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(i);
session.getTransaction().commit();
session.close();
System.out.println("Saved");
}
}
Album.java:
package templates;
public class Album {
private int ID;
private String title;
private String artist;
private String label;
public Album(){
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
Album.hbm.xml: link
Hibernate.cfg.xml: link
堆栈跟踪:
wrz 15, 2015 10:04:47 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.1.Final}
wrz 15, 2015 10:04:47 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
wrz 15, 2015 10:04:47 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
wrz 15, 2015 10:04:47 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
wrz 15, 2015 10:04:48 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
wrz 15, 2015 10:04:49 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
wrz 15, 2015 10:04:49 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Exception in thread "main" java.lang.ExceptionInInitializerError
at HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at HibernateUtil.<clinit>(HibernateUtil.java:7)
at test.main(test.java:13)
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at HibernateUtil.buildSessionFactory(HibernateUtil.java:12)
... 2 more
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
... 15 more
我做错了什么?
如错误所述,您需要在 hibernate.cfg 文件中指定方言。
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
我注意到一些可能导致问题的原因:
1) 在您的 hibernate.cfg.xml
中,您没有指定连接密码。您可以通过添加 <property name="connection.password">your_Pass_here</property>
属性(或者出于隐私原因省略它?)
2) 正如@Prerak Tiwari 提到的,您没有指定方言。按照他提到的 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
属性
3) 我注意到您的属性名称与大多数人使用的名称不同。不要添加 ...name="hibernate.connection.driver_class"...
省略 hibernate.
部分,所以它应该如下所示:
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernatetest</property>
<property name="connection.username">root</property>
Your password property here...
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="/templates/Album.hbm.xml"/>
</session-factory>
希望其中一些对你有用:)
这就是我修复错误的方法。
我的环境和你的一样Hibernate 5+
, Mysql
& WAMP Server
- 确保包含
jta jar
,它包含在此位置\hibernate-release-5.0.5.Final\lib\osgi
- 您应该添加新用户并授予所有权限。主机应该是
localhost
- 你的 hibernate.cfg.xml 应该是这样的
这就是它对我有用的方式。我想我必须给 localhost
而不是 %
我遇到了同样的问题。只需在 localhost 之后添加您的端口号。在我的例子中它是 localhost:3306
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_db</property>
检查密码是否正确 hibernate.cfg.xml
<property name="connection.password">root</property>