是的,我已经看到了这个问题,但我仍然无法弄清楚我的代码有什么问题。当然我在忽略一些东西,不能告诉我们什么。我当然可以使用第二只眼睛。
这是我的POJO。
data
下面是我的hibernate.cfg.xml
package hibernate;
public class User {
private int id;
private String name;
private int total;
private int goal;
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 int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getGoal() {
return goal;
}
public void setGoal(int goal) {
this.goal = goal;
}
}
这是我的User.hbm.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 name="">
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=test;</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">admin</property>
<mapping resource="hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这是将上述内容放在一起的主要应用程序:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 18, 2016 12:10:00 AM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="hibernate.User" table="USER">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="total" type="int">
<column name="TOTAL" />
</property>
<property name="goal" type="int">
<column name="GOAL" />
</property>
</class>
</hibernate-mapping>
当我运行上述内容时,我会得到以下异常。
package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class App {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
session.beginTransaction();
User user = new User();
user.setName("Joe");
user.setGoal(250);
session.save(user);
session.getTransaction().commit();
session.close();
HibernateUtility.getSessionFactory().close();
}
}
我见过很多人都有这个问题,但仍然无法弄清楚我的问题。非常感谢你的帮助。
答案 0 :(得分:0)
我认为错误在于这一行:
<class name="hibernate.User" table="USER">
将hibernate.User更改为User,它应该有帮助。
答案 1 :(得分:0)
您的会话工厂配置为incorrect for Hibernate 5。如果您使用Hibernate 5,则可以通过这种方式创建会话工厂
sessionFactory = new Configuration().configure().buildSessionFactory();
在文件User.hbm.xml
中,您不应该使用命名空间
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
使用
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"
而不是