Hiii伙计们 我正在构建一个简单的ToDoList(这只是我的项目的开始),它应该使用MAMP作为DB将任务添加到我的日程表中。 我的问题是season.save()缺少查询中的主键(“id”); 我有以下代码: 主要课程:
package Model;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
//create exception instead of HibernateException ToDoListDAOException
public class HibernateToDoListDAO implements IToDoListDAO {
SessionFactory factory = null;
private static final HibernateToDoListDAO _instanceDAO = new HibernateToDoListDAO();
// no need to do connection or use driver to my sql
private HibernateToDoListDAO(){
factory = new AnnotationConfiguration().configure().buildSessionFactory();
}
public static HibernateToDoListDAO getInstance()
{
return _instanceDAO;
}
@Override
public void addMemorandums(items memorandum) throws HibernateException {
Session session = factory.openSession();
try{
session.beginTransaction();
//create the query for memorandum members
session.saveOrUpdate(memorandum);
System.out.println("after save : user id's = "+memorandum.getId());
session.getTransaction().commit();
}catch(HibernateException e )
{
e.printStackTrace();
if ( session.getTransaction() != null )
session.getTransaction().rollback();
}finally{
session.close();
}
}
@Override
public void updateMemorandums(items memorandum) throws HibernateException {
Session session = null;
try{
session = factory.openSession();
session.beginTransaction();
session.update(memorandum);
session.getTransaction().commit();
}catch(HibernateException e )
{
e.printStackTrace();
if ( session.getTransaction() != null )
session.getTransaction().rollback();
}finally{
session.close();
}
}
@Override
public void deleteMemorandums(items memorandum) throws HibernateException {
Session session = null;
try{
session = factory.openSession();
session.beginTransaction();
session.delete(memorandum);
session.getTransaction().commit();
}catch(HibernateException e )
{
e.printStackTrace();
if ( session.getTransaction() != null )
session.getTransaction().rollback();
}finally{
session.close();
}
}
@SuppressWarnings("unchecked")
@Override
public List<items> getMemorandums() throws HibernateException {
List<items> memorandum = null;
Session session = null;
try{
session = factory.openSession();
session.beginTransaction();
memorandum = session.createQuery("from todolists").list();
}catch(HibernateException e )
{
e.printStackTrace();
if ( session.getTransaction() != null )
session.getTransaction().rollback();
}finally{
session.close();
}
return memorandum;
}
public static void main(String[] args) {
@SuppressWarnings("deprecation")
String dateTask = "26/11/2016";
items task1 = new items(2012,"football", dateTask , "ball");
HibernateToDoListDAO.getInstance().addMemorandums(task1);
}
}
项目类:
package Model;
import java.util.Date;
public class items {
private int id;// primary key that connect to the users
private String taskname;
private String date;
private String note;
public items(){
taskname = "basketball";
note = "a fun game";
date = "15/10/2016";
id = 2012;
}
public items(int id, String taskName, String taskDate, String note) {
super();
this.taskname = taskName;
this.date = taskDate;
this.note = note;
this.id = id;
}
public String getTaskname() {
return taskname;
}
public void setTaskname(String taskname) {
this.taskname = taskname;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
我正在尝试运行这个简单的程序,我得到的例外情况如下:
(例外)
Hibernate: insert into items (taskname, date, note) values (?, ?, ?)
org.hibernate.HibernateException: The database returned no natively generated identity value
at org.hibernate.id.IdentifierGeneratorHelper.getGeneratedIdentity(IdentifierGeneratorHelper.java:84)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:98)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:267)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:677)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:669)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:665)
at Model.HibernateToDoListDAO.addMemorandums(HibernateToDoListDAO.java:36)
at Model.HibernateToDoListDAO.main(HibernateToDoListDAO.java:111)
hibernate.cfg.xml中:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/hit
</property>
<property name="hibernate.connection.username">hit</property>
<property name="hibernate.connection.password">hit</property>
<property name="show_sql">true</property>
<!-- Mapping files -->
<mapping resource="hibernate.mapping.xml"/>
</session-factory>
</hibernate-configuration>
hibernate.mapping.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- *********** items table ******* -->
<!-- full class name including the packege name -->
<class name="Model.items" table="items">
<meta attribute="class-description">
This class contains the the items details.
</meta>
<id name="id" type="int" column="id"> <!-- primary key -->
<generator class="native"/>
</id>
<property name="taskname" column="taskname" type="string"/>
<property name="date" column="date" type="string"/>
<property name="note" column="note" type="string"/>
</class>
<!-- *********** users table ******* -->
<!-- full class name including the packege name -->
<class name="Model.users" table="users">
<meta attribute="class-description">
This class contains the users details.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
我正在使用mamp作为我的数据库(使用mysql)。 问题是查询只插入三个而不是四个变量(缺少“id”成员)。 有人可以帮我解决这个问题吗?
谢谢你们。答案 0 :(得分:1)
您必须自动增加表的标识列才能避免此错误。编写alter table查询以使标识列自动递增。这应该可以解决你的问题