第一个hibernate例子出错。。。(大佬们进来捡分啊)

阿Giao 2017-12-30 05:34:50
在慕课上照着老师的例子敲的代码但是报错,看了很久,但是不知道怎么改,各位大佬救救我啊 啊啊
持久化类:
public class User {
private String userName;
private String password;
private int team_id;

public User() {

}

public User(String userName, String password, int team_id) {
// super();

this.userName = userName;
this.password = password;
this.team_id = team_id;
}

@Override
public String toString() {
return "User [userName=" + userName + ", password=" + password + ", team_id=" + team_id + "]";
}


public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public int getTeam_id() {
return team_id;
}

public void setTeam_id(int team_id) {
this.team_id = team_id;
}

}

hibernate.cfg.xml的代码:
[code=java
]<?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>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
[/code]
UserTest代码:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

//测试类
public class UserTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;

@Before
public void init(){
//创建配置对象
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//打开会话
session = sessionFactory.openSession();
//开启事务
transaction = session.beginTransaction();
}

@After
public void destore() {
transaction.commit();// 提交事务
session.close();// 关闭会话
sessionFactory.close();// 关闭会话工厂
}


@Test
public void testSaveUser(){
//生成学生对象
User u = new User("hiber","123456",1);
session.save(u);//保存对象进入数据库
}
}

User.hbm.xml生成的,代码没改。
这个是结果框的内容:
十二月 30, 2017 5:32:01 下午 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
十二月 30, 2017 5:32:01 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.4.Final}
十二月 30, 2017 5:32:01 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十二月 30, 2017 5:32:01 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
十二月 30, 2017 5:32:01 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
十二月 30, 2017 5:32:01 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml


...全文
399 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿Giao 2017-12-31
  • 打赏
  • 举报
回复
引用 4 楼 net_lover 的回复:
报什么错误?上面的日志没有error啊,都是info或warning

这是junit跑的时候报的
孟子E章 2017-12-31
  • 打赏
  • 举报
回复
报什么错误?上面的日志没有error啊,都是info或warning
业余草 2017-12-31
  • 打赏
  • 举报
回复
阿Giao 2017-12-31
  • 打赏
  • 举报
回复
引用 4 楼 net_lover 的回复:
报什么错误?上面的日志没有error啊,都是info或warning
我知道了,谢谢了 User.hbm.xml错了(代码没贴出来)。我晕,路径前面多了个包名,但是我放在默认包里面的。我也不知道怎么回事
阿Giao 2017-12-30
  • 打赏
  • 举报
回复
引用 1 楼 net_lover 的回复:
<property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property> 应该写成 <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property> xml中特殊字符应该转义
不行,还是报错 十二月 30, 2017 6:41:35 下午 org.hibernate.annotations.common.Version <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} 十二月 30, 2017 6:41:35 下午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.2.4.Final} 十二月 30, 2017 6:41:35 下午 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 十二月 30, 2017 6:41:35 下午 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 十二月 30, 2017 6:41:35 下午 org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 十二月 30, 2017 6:41:35 下午 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 十二月 30, 2017 6:41:35 下午 org.hibernate.cfg.Configuration addResource INFO: HHH000221: Reading mappings from resource: User.hbm.xml 十二月 30, 2017 6:41:35 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 十二月 30, 2017 6:41:35 下午 org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null 十二月 30, 2017 6:41:35 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 十二月 30, 2017 6:41:35 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 20 十二月 30, 2017 6:41:35 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000006: Autocommit mode: false 十二月 30, 2017 6:41:35 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8] 十二月 30, 2017 6:41:35 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000046: Connection properties: {user=root, password=****} Sat Dec 30 18:41:35 CST 2017 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. 十二月 30, 2017 6:41:35 下午 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 十二月 30, 2017 6:41:35 下午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 十二月 30, 2017 6:41:35 下午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> INFO: HHH000397: Using ASTQueryTranslatorFactory
孟子E章 2017-12-30
  • 打赏
  • 举报
回复
<property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property> 应该写成 <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property> xml中特殊字符应该转义

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧