当前位置: 代码迷 >> J2EE >> 请问一下大家,hibernate初学有关问题
  详细解决方案

请问一下大家,hibernate初学有关问题

热度:78   发布时间:2016-04-17 23:04:32.0
请教一下大家,hibernate初学问题
这是项目结构


HibernateUtil.java
-------------------------------

package com.alex.utils;

import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class HibernateUtil {
private static SessionFactory sessionFactory = null;

private static SessionFactory buildSessionFactory() {
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build(); // configures
SessionFactory sf = null;
try {
sf = 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 sf;
}

public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
sessionFactory = buildSessionFactory();
}
return sessionFactory;
}
}

User.java
-------------------------------

package com.alex.entity;

public class User {

private int id;
private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}


TestUser.java
-------------------------------

package com.alex.service;

import org.hibernate.Session;
import com.alex.entity.User;
import com.alex.utils.HibernateUtil;

public class TestUser {

public static void main(String[] args) {
// TODO Auto-generated method stub
Session session = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
User user = new User();
user.setId(1);
user.setName("haha");
session.save(user);
session.getTransaction().commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
session.getTransaction().rollback();
} finally {
session.close();
HibernateUtil.getSessionFactory().close();
}
}

}


hibernate.cfg.xml
--------------------------------

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/sshtest</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
        <!-- SQL dialect-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
        <!-- Disable the second-level cache 
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> -->
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup-->
        <property name="hbm2ddl.auto">create</property> 
        <!-- Names the annotated entity class -->
        <mapping resource="com/alex/entity/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


user.hbm.xml
--------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC  
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
<class name="com.alex.bean.User" table="user">
<id name="id" column="id" type="int">
<generator class="increment"/>
</id>
<property name="name" column="name" type="string"/>
</class>
</hibernate-mapping>


出现下面的错误

九月 23, 2015 12:14:09 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.1.Final}
九月 23, 2015 12:14:09 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
九月 23, 2015 12:14:09 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
九月 23, 2015 12:14:09 上午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
九月 23, 2015 12:14:10 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
九月 23, 2015 12:14:10 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/sshtest]
九月 23, 2015 12:14:10 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
九月 23, 2015 12:14:10 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
九月 23, 2015 12:14:10 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
九月 23, 2015 12:14:10 上午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
九月 23, 2015 12:14:11 上午 org.hibernate.boot.model.source.internal.hbm.ModelBinder bindProperty
WARN: HHH90000003: Use of DOM4J entity-mode is considered deprecated
九月 23, 2015 12:14:11 上午 org.hibernate.boot.model.source.internal.hbm.ModelBinder bindProperty
WARN: HHH90000003: Use of DOM4J entity-mode is considered deprecated
九月 23, 2015 12:14:11 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/sshtest]
java.lang.NullPointerException
at com.alex.service.TestUser.main(TestUser.java:15)
Exception in thread "main" java.lang.NullPointerException
at com.alex.service.TestUser.main(TestUser.java:27)


请教一下大家!
------解决思路----------------------
id是自增的,不用set
  相关解决方案