错误发生在session.save(customer)行.CUSTOMER和EventRequest是表名。 custno。是CUSTOMER表和EventRequest表中的主键。
客户和EventRequest文件中的映射是否存在任何问题。
custno。在CUSTOMER表中不应为null,但hibernate不插入custno。在CUSTOMER TABLE中。这就是hibernate的作用:它缺少custno。柱。
插入CUSTOMER(内部,地址,城市,联系人,custname,phone,state,zip)值(?,?,?,?,?,?,?,?)
错误跟踪:
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
**Hibernate: insert into CUSTOMER (Internal, address, city, contact, custname, phone, state, zip) values (?, ?, ?, ?, ?, ?, ?, ?)**
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:61)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2975)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3487)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:214)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:194)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at com.sun.proxy.$Proxy12.save(Unknown Source)
at com.icdab.www.icdab_first.App.persistAnnotatedLists(App.java:67)
at com.icdab.www.icdab_first.App.main(App.java:89)
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("SYSTEM"."CUSTOMER"."CUSTNO")
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1017)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:655)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:249)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:566)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:215)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:58)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:943)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1075)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3820)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3897)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1361)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
“错误说不能插入NULL(”SYSTEM“。”CUSTOMER“。”CUSTNO“)”。 App.Java:
package com.icdab.www.icdab_first;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
/**
* Hello world!
*
*/
public class App
{
private static SessionFactory factory;
private static ServiceRegistry registry;
// private void init(){
// System.out.println("in init");
static {
Configuration config = new Configuration().configure("hibernate.cfg.xml");
/*.addAnnotatedClass(CompundKey.class)
.addAnnotatedClass(Customer.class)
.addAnnotatedClass(Employee.class)
.addAnnotatedClass(EventPlan.class)
.addAnnotatedClass(EventPlanLine.class)
.addAnnotatedClass(EventRequest.class)
.addAnnotatedClass(Facility.class)
.addAnnotatedClass(Location.class)
.addAnnotatedClass(ResourceTbl.class) */
// learn why did you use the annotatedclass and configuration.
registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
factory=config.buildSessionFactory(registry);
}
// }
@SuppressWarnings("deprecation")
private void persistAnnotatedLists(){
Configuration config = new Configuration().configure("hibernate.cfg.xml");
registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
factory=config.buildSessionFactory(registry);
// Session session_one = factory.getSessionFactory().openSession();
Session session = factory.getCurrentSession();
session.beginTransaction();
Customer customer = new Customer();
customer.setAddress("address123");
customer.setCity("lansing");
customer.setContact("1632 ne ave");
customer.setCustname("john");
customer.setCustno("1234");
customer.setInternal("internal");
customer.setPhone(122345679);
customer.setState("OR");
customer.setZip(97654);
System.out.println("the setter methods completed");
List<EventRequest> ers = new ArrayList<EventRequest>();
ers.add(new EventRequest(123,new Date(92,5,1),new Date(91,5,1),new Date(93,5,1),"av",123,456,124));
ers.add(new EventRequest(1234,new Date(92,5,2),new Date(91,5,2),new Date(93,5,2),"av",124,457,125));
customer.setEventrequests(ers);
//here is the error
session.save(customer);
System.out.println("befor commit");
session.getTransaction().commit();
System.out.println("after commit");
System.out.println("done persist");
}
private void retrieveList(){
Session session = factory.getCurrentSession();
session.beginTransaction();
List list = session.createQuery("from com.icdab.www.icdab_first.Customer").list();
for (Object object : list) {
System.out.println("** List items: "+object);
}
session.getTransaction().commit();
System.out.println("Done retrieve");
}
public static void main (String[] args){
App app = new App();
//app.init();
app.persistAnnotatedLists();
app.retrieveList();
}
}
Customer.java:
package com.icdab.www.icdab_first;
import javax.persistence.*;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import java.util.List;
import com.icdab.www.icdab_first.EventRequest;
@Entity
@Table(name="CUSTOMER")
public class Customer {
@Id
@Column(name="custno",nullable=false)
@GeneratedValue(strategy= GenerationType.IDENTITY)
private String custno;
private String custname;
private String address;
private String Internal;
private String contact;
private int phone;
private String city;
private String state;
private int zip;
//added block to define the onetomany relationship b.w tables.
@OneToMany
@JoinColumn(name="custno")
@Cascade(CascadeType.ALL)
private List<EventRequest> eventrequests;
public List<EventRequest> getEventrequests() {
return eventrequests;
}
public void setEventrequests(List<EventRequest> eventrequests) {
this.eventrequests = eventrequests;
}
public String getCustno() {
return custno;
}
public void setCustno(String custno) {
this.custno = custno;
}
public String getCustname() {
return custname;
}
public void setCustname(String custname) {
this.custname = custname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getInternal() {
return Internal;
}
public void setInternal(String internal) {
Internal = internal;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getZip() {
return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
}
EventRequest.java:
package com.icdab.www.icdab_first;
import java.sql.Date;
import java.sql.Time;
import javax.persistence.*;
import java.util.List;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import com.icdab.www.icdab_first.Customer;
@Entity
@Table(name="EVENTREQUEST")
public class EventRequest {
@Id
@Column(name="EVENTNO",nullable=false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int eventno;
private Date dateheld;
private Date datereq;
//private int facno;
private Date dateauth;
private String status;
private int estcost;
private int estaudience;
private int budno;
public EventRequest(){
}
public EventRequest(int eventno, Date dateheld,
Date datereq, Date dateauth,String status,
int estcost, int estaudience,int budno)
{
setEventno(eventno);
setDateheld(dateheld);
setDatereq(datereq);
setDateauth(dateauth);
setStatus(status);
setEstcost(estcost);
setEstaudience(estaudience);
setBudno(budno);
}
// one to many mapping eventrequest and eventplan
@OneToMany
@JoinColumn(name="eventno")
@Cascade(CascadeType.ALL)
private List<EventPlan> eventplans;
public List<EventPlan> getEventplans() {
return eventplans;
}
public void setEventplans(List<EventPlan> eventplans) {
this.eventplans = eventplans;
}
public int getEventno() {
return eventno;
}
public void setEventno(int eventno) {
this.eventno = eventno;
}
public Date getDateheld() {
return dateheld;
}
public void setDateheld(Date dateheld) {
this.dateheld = dateheld;
}
public Date getDatereq() {
return datereq;
}
public void setDatereq(Date datereq) {
this.datereq = datereq;
}
/*
public int getCustno() {
return custno;
}
public void setCustno(int custno) {
this.custno = custno;
}
*/
/*
public int getFacno() {
return facno;
}
public void setFacno(int facno) {
this.facno = facno;
}
*/
public Date getDateauth() {
return dateauth;
}
public void setDateauth(Date dateauth) {
this.dateauth = dateauth;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getEstcost() {
return estcost;
}
public void setEstcost(int estcost) {
this.estcost = estcost;
}
public int getEstaudience() {
return estaudience;
}
public void setEstaudience(int estaudience) {
this.estaudience = estaudience;
}
public int getBudno() {
return budno;
}
public void setBudno(int budno) {
this.budno = budno;
}
}
答案 0 :(得分:0)
GenerationType.IDENTITY
表示持久性提供程序必须使用数据库标识列为实体分配主键。没有这样的东西叫做#34;身份&#34;从Oracle 11g开始的Oracle中的列(12c中可用)。因此,在您的情况下,您必须创建数据库序列,然后使用@GeneratedValue(strategy=GenerationType.AUTO)
或@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
为主键字段生成值。