这两天一直在解决这个问题,也查阅了很多资料,实在是心酸的不行。现在终于调试出来了,这么过分一定要发博客!
首先先说明一下,我用的是Hibernate 5.2和MySQL 5.7的版本!
划重点!!——————————————————————————————*
不同的版本之间用的方法是不同的!楼主就是死在这些不同的版本使用不同的方法和名称上面!请各位程序员一定要确定好自己的Hibernate、SQL版本!不然就会像我这样,死了很多次都死不出来。
——————————————————————————————————–
首先我参考了很多地方,关于not found问题的话总结以下2点:
1如果代码没有逻辑和语法错误,但是出现了NOT FOUND 提示
.你可以不用管这个提示(前提是在你所有的代码都正确,cfg文件关联也正确的情况下)因为我现在发现之前我错的时候以及现在修改正确的时候,这个提示也是仍旧存在的。你可以把它当做是一个warning ,而不是一个必须修改的error.
因为在Hibernate里面,properties文件和cfg.xml文件都是用来配置的,但是如果Hibernate文件是默认先看xml的配置的,如果有xml文件的话就不会去看Properties了,properties文件是在比较老的hibernate版本里使用的。所以我们配置了xml文件就可以不用管这个提示了。就像我现在成功了之后照样有这个提示一样。
2 如果代码有错误的话,那请参考以下操作。
因为我不确定你的错误在哪些地方,所以我把我自己的配置文件什么的贴出来 自行对照。
2.1首先你的文件位置一定要摆放好。
student.hbm.xml文件和student.java文件最好是放在一个包下面。然后hibernate.cfg.xml一定是放在src文件下。因为在创建回话工厂的时候如果你没有写cfg文件的路径,系统是默认去src文件下寻找配置文件的。(如果自己喜欢写路径的朋友,那就没问题了,反正我是不会写路径的……比较麻烦还容易出错)
因此我的文件摆放如图所示
记得要导入hibernate/required文件下的所有jar包和mysql的驱动哟。
2.2 hibernate.cgf.xlm配置
<?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/db</property>
<property name="connection.username">root</property>
<property name="connection.password">111111</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect 注意新版的MYSQL5.7的方言是org.hibernate.dialect.MySQL5InnoDBDialect 不要弄错了!这里错了也出不来的 -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</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="hibernate.format_sql">true</property>
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<property name="current_session_context_class">thread</property>
<mapping class="config.student"/>
<mapping resource="config/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
配置文件里要注意的地方就是
<mapping resource="config/student.hbm.xml"/>和
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
这两句话了。MYSQL5.7的方言表达一定要注意。而配置文件的关联记得包下的文件之间是用斜杆而不是点
2.3 student.hmb.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">
<!-- Generated 2018-8-23 13:22:25 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="config.student" table="student">
<id name="id" >
<column name='s_id'></column>
</id>
<property name="name" >
<column name="name"></column>
</property>
<property name="sex" >
<column name='sex'></column>
</property>
</class>
</hibernate-mapping>
这里要注意的是这句话
“http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd“>
网上我们找到的版本基本都是
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“>
我在调试的时候发现提示是说第二个表达已经被抛弃了 ,现在我们用的是第一个。
2.4 student.java
package config;
public class student {
private int id;
private String name;
private String sex;
public student(int a,String b,String c)
{
this.id=a;
this.name=b;
this.sex=c;
}
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
2.5 studenttest.java
package test;
import static org.junit.Assert.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import config.student;
public class studenttest {
@Test
public void testSaveStudents() {
student student = new student(2,"皮皮猪","female");
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
session.close();
sf.close();
}
}
这里最最最重要的就是创建会话工厂了。
不同的版本创建的方法也是完全不一样的。hibernate 4\5的创建方法又完全不一样。有些方法已经废弃了。在这里我肯保证的是这个方法对于Hibernate5.2的版本绝对能用,5.3应该也可以但是我没试过。
下面我贴出来官方的创建方法(我用官方用户指南里的方法反而创建不成功回话工厂,这让我非常的惆怅。大家可以去试试,如果成功了请告诉一下我谢谢!)
Example 4. Obtaining the org.hibernate.SessionFactory
protected void setUp() throws Exception {
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
try {
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 );
}
}
Example 6. Obtaining a list of entities
session = sessionFactory.openSession();
session.beginTransaction();
List result = session.createQuery( "from Event" ).list();
for ( Event event : (List<Event>) result ) {
System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
}
session.getTransaction().commit();
session.close();
如果大家看我说的不太明白的话 ,这里请参考一下这个优秀的同学写的哟~
https://bbs.csdn.net/topics/391955731