最初のHibernateアプリケーションのセットアップに数時間を費やしましたが、それでも機能しません。
「hibernatetest」と呼ばれるMySQLデータベースを備えたWAMPサーバーがあります。 Hibernateライブラリを含むEclipseのProjectと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(mainを含む):
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: リンク
Hibernate.cfg.xml: リンク
スタックトレース:
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
私は何が間違っているのですか?
私も同じ問題を抱えていました。 localhostの後にポート番号を追加するだけです。私の場合はlocalhost:3306です。
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_db</property>
エラーが示すように、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.cfg.xml
でパスワードが正しいかどうかを確認します
<property name="connection.password">root</property>