2016-01-23 65 views
0

我已經附加了我的代碼,它根本沒有創建sessionfactory,請看看它,讓我知道我錯了什麼地方。會話工廠創建使用註冊表和元數據的Hibernate 5.0.7

HibernateUtilities.java

package com.yashwanth.HIbernate; 

import org.hibernate.HibernateException; 
import org.hibernate.SessionFactory; 
import org.hibernate.boot.Metadata; 
import org.hibernate.boot.MetadataSources; 
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl; 
import org.hibernate.boot.registry.StandardServiceRegistry; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 

public class HIbernateUtilities { 
private static SessionFactory sessionFactory; 
private static ServiceRegistry serviceRegistry; 

public SessionFactory setUp() throws Exception 
{ 

     final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() 
       .configure() // configures settings from hibernate.cfg.xml 
       .build(); 

      sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); 

     /*catch (Exception e) { 
      // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory 
      // so destroy it manually. 
      StandardServiceRegistryBuilder.destroy(registry); 
     }*/ 
return sessionFactory; 
} 
} 

這裏是另外一個Java類:提前

package com.yashwanth.HIbernate; 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
public class Program { 
public static void main(String[] args) throws HibernateException, Exception 
    { 
     System.out.println("yashpotu"); 
     HIbernateUtilities Utility =new HIbernateUtilities(); 
     SessionFactory sessionFactory=Utility.setUp(); 

     Session session = sessionFactory.openSession(); 
     session.beginTransaction(); 
     User user=new User(); 
     user.setName("YASH"); 
     user.setGoal(250); 
     session.save(user); 
     session.getTransaction().commit(); 
     session.close(); 
     sessionFactory.close(); 
     } 

     } 

Here is the error:an 23, 2016 1:09:34 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {5.0.7.Final} 
Jan 23, 2016 1:09:34 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Jan 23, 2016 1:09:34 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Jan 23, 2016 1:09:35 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 
Jan 23, 2016 1:09:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) 
Jan 23, 2016 1:09:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306] 
Jan 23, 2016 1:09:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 
INFO: HHH10001001: Connection properties: {user=root, password=****} 
Jan 23, 2016 1:09:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 
INFO: HHH10001003: Autocommit mode: false 
Jan 23, 2016 1:09:35 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init> 
INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 
Sat Jan 23 13:09:35 EST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 
Jan 23, 2016 1:09:35 PM org.hibernate.dialect.Dialect <init> 
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 
Exception in thread "main" org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.yashwanth.HIbernate.User] 
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:229) 
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.<init>(AnnotationMetadataSourceProcessorImpl.java:103) 
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.<init>(MetadataBuildingProcess.java:147) 
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:141) 
    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.boot.MetadataSources.buildMetadata(MetadataSources.java:179) 
    at com.yashwanth.HIbernate.HIbernateUtilities.setUp(HIbernateUtilities.java:48) 
    at com.yashwanth.HIbernate.Program.main(Program.java:10) 
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.yashwanth.HIbernate.User 
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:217) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Class.java:348) 
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:226) 
    ... 9 more 

謝謝,請讓我知道我在哪裏出了錯

+0

好了,消息說:「無法加載請求的類:org.yashwanth.HIbernate.User」。你是否在類路徑中有這個類? –

+0

線程「main」中的異常org.hibernate.boot.registry.classloading.spi.ClassLoadingException:無法加載類[org.yashwanth.HIbernate.User] –

+0

yes加載類本身時出現問題 –

回答

0
try { 
     Configuration configuration = new Configuration().configure(); 

     ServiceRegistry registry = new StandardServiceRegistryBuilder() 
       .applySettings(configuration.getProperties()).build(); 
     return new MetadataSources(registry).buildMetadata().buildSessionFactory(); 
    } catch (Throwable cause) { 
     System.err.println("Initial SessionFactory creation failed: " + cause); 
     throw new ExceptionInInitializerError(cause); 
    }