第一个Hibernate出现的问题

Defens 发布于 2015/04/25 17:21
阅读 164
收藏 0

Hibernate版本:hibernate-release-4.2.19.Final

程序保存一个User到数据库中:程序如下:

User实体类:

package com.defens.hello;

public class User {
	private long id;
	private String name;
	private String password;
	private int age;
	
	public User() {
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}



UserDao类:

package com.defens.hello;

public interface IUserDao {
	public void save(User user);
	public void update(User user);
	public User get(long id);
	public void delete(long id);
	
}



实现类:

package com.defens.hello;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class UserDaoImpl implements IUserDao{
	@Override
	public void save(User user) {
		//1、读取配置文件,会自动读取
		Configuration config = new Configuration().configure();
		//通过配置对象得到SessionFacyory对象
		SessionFactory factory = config.buildSessionFactory();
		//获取Session
		Session session = factory.openSession();
		//保存对象
		//Hibernate在默认情况下不会自动提交
		//手动提交事务
		Transaction tran = session.beginTransaction();//给Session设置一个开始标记
		session.save(user);
		//提交事务
		tran.commit();
		//关闭资源
		session.close();
		factory.close();
	}

	@Override
	public void update(User user) {
		
	}

	@Override
	public void delete(long id) {
		
	}

	@Override
	public User get(long id) {
		
		return null;
	}

}



测试类:

package com.defens.hello;

import org.junit.Test;

public class UserDaoTest {
	
	private IUserDao dao = new UserDaoImpl();
	
	@Test
	public void testSave() {
		User user = new User();
		//user.setId(1);
		user.setName("Defens");
		user.setPassword("123456");
		user.setAge(23);
		
		dao.save(user);
	}	
}



配置文件:User.hbm.xml

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

<hibernate-mapping package="com.defens.hello">
	<!-- table属性指定对应的表明 -->
	<class name="User" table="Users">
		<!-- id属性指定主键,name属性告知对象的属性和主键的对应关系, column告知列名 -->
		<id name="id" column="id">
			<generator class="native"></generator>
		</id>
		<!-- 属性和表列明配置, name的值对应对象的属性, column对应数据库的列 -->
		<property name="name" column="name"/>
		<property name="password" column="password"/>
		<property name="age" column="age"/>
	</class>
</hibernate-mapping>



hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<!-- Hibernate配置 -->
<hibernate-configuration>
	<!-- Session工厂    先理解为DateSource -->
	<session-factory >
		<!-- 设置Hibernate打印生成的SQL -->
		<property name="show_sql">true</property>
		<!-- 配制连接参数  hibernate.是可以省略不写的,但是在其他方法配制的时候不能省略 -->
		<!-- 配制数据库方言, 即识别数据库的参数 该配置屏蔽了不同数据库在sql的实现细节 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hibernate</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password"></property>
		
		<!-- 
			1、Hibernate应该能够处理哪些持久化对象的处理
			2、Hibernate应该能够怎么帮我保存对象,如属性和列的对应关系
		 -->
		<!-- 解决问题二,告诉Hibernate要管理的配置文件 -->
		<mapping resource="com/defens/hello/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>



程序结构图片:

错误:

四月 25, 2015 5:13:12 下午 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
四月 25, 2015 5:13:12 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.19.Final}
四月 25, 2015 5:13:12 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
四月 25, 2015 5:13:12 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
四月 25, 2015 5:13:12 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
四月 25, 2015 5:13:12 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml


图片:

刚开始使用这个框架,很多东西不熟悉,所以出的错也难得找到解决的思路,求指导。

加载中
0
Defens
Defens
知道了,hibernate.cfg.xml要放在src下
0
未来守护者
未来守护者
: hibernate.properties not found
返回顶部
顶部