今天打算编写一个测试类来试试昨天搭建好的Hibernate框架。
首先是TestInsert.java
package com.Lily.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.Lily.Utils.HibernateUtils;
import com.Lily.db.User;
public class TestInsert {
public static void main(String[] args) {
User us=new User();
us.setUsername("老王");
us.setPassword("123");
us.setGender("男");
Session sess=HibernateUtils.getSession();
Transaction tran =sess.beginTransaction();
sess.save(us);
tran.commit();
sess.close();
System.out.println("OK");
}
}
书上的示例只有这些,但是我发现什么HibernateSessionFactory根本就没有。
然后百度,感觉得自己写一个util工具类才行:
package com.Lily.Utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory factory;
static{
//读取cfg.xml文件
Configuration cfr = new Configuration().configure();
//建立SessionFactory
factory=cfr.buildSessionFactory();
}
//获得开启的Session
public static Session getSession(){
return factory.openSession();
}
//关闭Session
public static void closeSession(Session session){
if(session!=null){
if(session.isOpen()){
session.close();
}
}
}
public static SessionFactory getSessionFactory(){
return factory;
}
}
Debug心得:
Configuration cfr = newConfiguration().configure();
默认读取hibernate.cfg.xml文件 的时候报错了,说Cannot instantiate the type Configuration。
【答案是这个:https://zhidao.baidu.com/question/1382172486117898220.html】
我居然,把包导错了….这真是尴尬的呵呵。
书上说,如果只是save,那么Hibernate只会缓存,不会真的修改数据库。
所以我就写了tran.coommit()
但是我发现我的数据里并没有改变。
看日志,好像除了SSL并没有什么错。
先改SSL吧。
三月 20, 2017 9:03:20 下午 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate CommonsAnnotations {4.0.1.Final}
三月 20, 2017 9:03:20 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core{4.1.4.Final}
三月 20, 2017 9:03:20 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.propertiesnot found
三月 20, 2017 9:03:20 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode providername : javassist
三月 20, 2017 9:03:20 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring fromresource: /hibernate.cfg.xml
三月 20, 2017 9:03:20 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configurationresource: /hibernate.cfg.xml
三月 20, 2017 9:03:20 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings fromresource: com/Lily/db/User.hbm.xml
三月 20, 2017 9:03:20 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings fromresource: ./user.hbm.xml
三月 20, 2017 9:03:20 下午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: ConfiguredSessionFactory: null
三月 20, 2017 9:03:20 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigure
INFO: HHH000402: Using Hibernatebuilt-in connection pool (not for production use!)
三月 20, 2017 9:03:20 下午org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigure
INFO: HHH000115: Hibernate connectionpool size: 20
三月 20, 2017 9:03:20 下午org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigure
INFO: HHH000006: Autocommit mode:false
三月 20, 2017 9:03:20 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigure
INFO: HHH000401: using driver[com.mysql.jdbc.Driver] at URL[jdbc:mysql://localhost:3306/bookStore?characterEncoding=utf-8]
三月 20, 2017 9:03:20 下午org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigure
INFO: HHH000046: Connectionproperties: {user=root, password=****}
Mon Mar 20 21:03:20 CST 2017 WARN:Establishing SSL connection without server's identity verification is notrecommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSLconnection must be established by default if explicit option isn't set. Forcompliance with existing applications not using SSL the verifyServerCertificateproperty is set to 'false'. You need either to explicitly disable SSL bysetting useSSL=false, or set useSSL=true and provide truststore for servercertificate verification.
三月 20, 2017 9:03:20 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect:org.hibernate.dialect.MySQLDialect
三月 20, 2017 9:03:21 下午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiatorinitiateService
INFO: HHH000399: Using defaulttransaction strategy (direct JDBC transactions)
三月 20, 2017 9:03:21 下午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: UsingASTQueryTranslatorFactory
好吧。再改。
Caused by: org.dom4j.DocumentException: Error on line 10of document : 对实体 "useSSL" 的引用必须以 ';' 分隔符结尾。 Nested exception: 对实体 "useSSL"的引用必须以 ';' 分隔符结尾。
atorg.dom4j.io.SAXReader.read(SAXReader.java:482)
atorg.hibernate.cfg.Configuration.doConfigure(Configuration.java:2009)
... 4more
然后我学网上的方法加了一个&.
http://blog.csdn.net/qq542045215/article/details/45313507
继续报错….好心累!!
The connection property 'useSSL' only acceptsvalues of the form: 'true', 'false', 'yes' or 'no'. The value 'true;' is not inthis set.
<property name="connection.url">jdbc:mysql://localhost:3306/bookStore?characterEncoding=utf-8&useSSL=true;</property>
改成:
<property name="connection.url">jdbc:mysql://localhost:3306/bookStore?useSSL=true&characterEncoding=utf-8</property>
然后就不报错了。我能说什么呢?~
最后我发现,其实不改SSL应该也修改了数据库的,
这里我必须吐槽一下,我在mydql command line client里面select的时候确实应该是每次都执行了
但是我如果用mysql workbench的话,我在workbench里面insert的东西 command select 不到, 而且自己也select 不到程序insert进去的东西。
最后我想说,怪不得人家说看书学的快。
书上真是言简意赅,说什么都只说一句,其他的全靠悟(百度)