org.hibernate.PropertyValueException:not-null属性引用一个null或瞬时值:com.Attendance.Batch.BCode 我需要以简单的struts2方式将数据插入数据库中
Batch.java // pojo类
package com.Attendance;
import javax.persistence.*;
@Entity
@Table(name="BatchTimeTable")
public class Batch{
@Id
@GeneratedValue
@Column(name="id")
private int id;
@Column(name="Batch_Code",unique=true,nullable = false)
private String BCode;
@Column(name="ClassRoom",nullable = false)
private String CRoom;
@Column(name="Batch_Time",nullable = false)
private String BTime;
@Column(name="Class_Name",nullable = false)
private String CName;
//getter and setter
BatchADD.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="Navbar.css">
<link rel="stylesheet" href="AStyle.css">
<script type="text/javascript" src="Script.js">
</script>
<link href="<s:url value ="/css/AStyle.css"/>" rel="stylesheet"
type="text/css" />
<title>BATCH</title>
</head>
<body>
<%
String Name = (String) session.getAttribute("name");
%>
<s:form action="BatchInserting">
<table>
<tr>
<td>
<s:textfield name="BCode" label="Batch Code"/></td></tr>
<tr>
<td>
<s:textfield name="CRoom" label="LAB/ClassRoom"/></td></tr>
<tr>
<td><s:textfield name="BTime" label="Lecturer Time" /></td></tr>
<tr>
<td>
<s:hidden value="%{#session['name']}" name="CName" /></td</tr>
<tr>
<td>
<s:submit value="Insert New Batch"></s:submit></td></tr>
</table>
</s:form>
</body></html>
BatchAction.java //动作类
package com.Attendance;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("serial")
public class BatchAction extends ActionSupport implements ModelDriven<Batch>{
private Batch b=new Batch();
@Override
public Batch getModel() {
// TODO Auto-generated method stub
return null;
}
public String saveB() {
boolean ba=DaoBatch.save(b);
if(ba==true) {
System.out.println("Inserting");
return SUCCESS;
}
return ERROR;
}
}
DaoBatch.java
package com.Attendance;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class DaoBatch {
public static boolean save(Batch b){
boolean flag=true;
SessionFactory factory= new Configuration().configure().buildSessionFactory();
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
try {
session.save(b);
tx.commit();
}catch(Exception e) {
flag=false;
e.printStackTrace();
}
session.close();
return flag;
}
}
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="com.Attendance" extends="struts-default">
<!--Batch model-->
<action name="BatchInserting" class="com.Attendance.BatchAction" method="saveB">
<result name="success">/Admin/success.jsp</result>
<result name="error">/Admin/RegistrationTeacher.jsp</result>
<result name="input">/Admin/RegistrationTeacher.jsp</result>
</action>
<!--Teacher model-->
<action name="RegTeacher" class="com.Attendance.TeacherAction" method="save">
<result name="success">/Admin/success.jsp</result>
<result name="error">/Admin/RegistrationTeacher.jsp</result>
<result name="input">/Admin/RegistrationTeacher.jsp</result>
</action>
<action name="Show" class="com.Attendance.TeacherAction" method="List">
<result name="success">/Admin/ShowAllTeacher.jsp</result>
<result name="input">/Admin/ShowAllTeacher.jsp</result>
</action>
<action name="EditTeacher" class="com.Attendance.TeacherAction" method="FindById">
<result name="success">/Admin/UDTeacher.jsp</result>
<result name="input">/Admin/ShowAllTeacher.jsp</result>
</action>
<action name="UDTeacher" class="com.Attendance.TeacherAction" method="Update">
<result name="success">/Admin/success.jsp</result>
<result name="error">/Admin/UDTeacher.jsp</result>
<result name="input">/Admin/UDTeacher.jsp</result>
</action>
<action name="Delete" class="com.Attendance.TeacherAction" method="Delete">
<result name="success">/Admin/success.jsp</result>
<result name="error">/Admin/Show.jsp</result>
<result name="input">/Admin/Show.jsp</result>
</action>
<action name="LoginTeacher" class="com.Attendance.TeacherAction" method="LoginT">
<result name="success">/Teacher/HomeT.jsp</result>
<result name="error">/Teacher/LoginTeacher.jsp</result>
<result name="input">/Teacher/LoginTeacher.jsp</result>
</action>
<action name="logout" class="com.Attendance.TeacherAction" method="logout">
<result name="success">/Teacher/CheckLoginT.jsp</result>
</action>
</package>
<constant name="struts.action.excludePattern" value="/CheckCode,/CheckRegistrationAdmin,/LoginAdmin,"/>
</struts>
hibernate.cfg.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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/attendance</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- List of XML mapping files -->
<mapping class="com.Attendance.RegistrationAdmin"/>
<mapping class="com.Attendance.DaoAdmin"/>
<mapping class="com.Attendance.RegTeacher"/>
<mapping class="com.Attendance.DaoTeacher"/>
<mapping class="com.Attendance.Batch"/>
<mapping class="com.Attendance.DaoBatch"/>
</session-factory>
</hibernate-configuration>
错误
org.hibernate.PropertyValueException: not-null property references a null or transient value: com.Attendance.Batch.BCode
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:100)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:312)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
at com.Attendance.DaoBatch.save(DaoBatch.java:18)
at com.Attendance.BatchAction.saveB(BatchAction.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
我需要在数据库中简单插入数据
答案 0 :(得分:0)
我遇到错误
getmodel(){是一个空值,返回null;}这是错误的,因此请更改return b;。 b是对象批处理