这是我的User.java
@Entity
@Table(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int id;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "EMAIL")
private String email;
@OneToMany(cascade={CascadeType.ALL})
@JoinColumn(name="USER_ID")
private Collection<Address> addresses;
}
这是我的Address.java
@Entity
@Table(name = "address")
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int id;
@Column(name = "TOWN")
private String town;
@Column(name = "DISTRICT")
private String district;
@Column(name = "COUNTRY")
private String country;
@ManyToOne
@JoinColumn(name = "USER_ID", insertable=false, updatable=false, nullable=false)
private User user;
}
但是当我试图坚持这个对象时,我得到了这个异常
javax.servlet.ServletException: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert: [com.kpowd.model.Address]
javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
root cause
javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert: [com.kpowd.model.Address]
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:798)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1331)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:181)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:645)
root cause
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert: [com.kpowd.model.Address]
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1315)
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1321)
org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:843)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
com.sun.proxy.$Proxy19.persist(Unknown Source)
com.kpowd.dao.basedao.GenericDaoImpl.create(GenericDaoImpl.java:47)
com.kpowd.service.impl.UserServiceImpl.registerUser(UserServiceImpl.java:34)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
com.sun.proxy.$Proxy20.registerUser(Unknown Source)
com.kpowd.controller.UserRegistrationBean.register(UserRegistrationBean.java:80)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.apache.el.parser.AstValue.invoke(AstValue.java:278)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:798)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1331)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:181)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:645)
root cause
org.hibernate.exception.GenericJDBCException: could not insert: [com.kpowd.model.Address]
org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2345)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2852)
org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:799)
org.hibernate.impl.SessionImpl.persist(SessionImpl.java:791)
org.hibernate.engine.EJB3CascadingAction$1.cascade(EJB3CascadingAction.java:48)
org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:425)
org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:362)
org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:338)
org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
org.hibernate.engine.Cascade.cascade(Cascade.java:161)
org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:475)
org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:353)
org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:837)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
com.sun.proxy.$Proxy19.persist(Unknown Source)
com.kpowd.dao.basedao.GenericDaoImpl.create(GenericDaoImpl.java:47)
com.kpowd.service.impl.UserServiceImpl.registerUser(UserServiceImpl.java:34)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
com.sun.proxy.$Proxy20.registerUser(Unknown Source)
com.kpowd.controller.UserRegistrationBean.register(UserRegistrationBean.java:80)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.apache.el.parser.AstValue.invoke(AstValue.java:278)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:798)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1331)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:181)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:645)
root cause
java.sql.SQLException: Field 'USER_ID' doesn't have a default value
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2345)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2852)
org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:799)
org.hibernate.impl.SessionImpl.persist(SessionImpl.java:791)
org.hibernate.engine.EJB3CascadingAction$1.cascade(EJB3CascadingAction.java:48)
org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:425)
org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:362)
org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:338)
org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
org.hibernate.engine.Cascade.cascade(Cascade.java:161)
org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:475)
org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:353)
org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:837)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
com.sun.proxy.$Proxy19.persist(Unknown Source)
com.kpowd.dao.basedao.GenericDaoImpl.create(GenericDaoImpl.java:47)
com.kpowd.service.impl.UserServiceImpl.registerUser(UserServiceImpl.java:34)
如果我删除&#34; NOTNULL&#34>,则不会出现错误来自FORIGN_KEY&#34; USER_ID&#34;的约束 但我需要把它作为一个NOTNULL列,我该怎么做呢。请帮帮我??
这是我的注册方式
public String register() {
// Output some info
Address a = new Address();
a.setCountry("country1");
a.setDistrict("district1");
a.setTown("town");
Address b = new Address();
b.setCountry("country2");
b.setDistrict("district2");
b.setTown("town2");
List<Address> adrlist=new ArrayList<Address>();
adrlist.add(a);
adrlist.add(b);
User user = new User();
user.setFirstName("firstname");
user.setLastName("lastname");
user.setEmail("email");
user.setAddresses(adrlist);
// Call the business object to register the user
userService.registerUser(user);
// Set the message here
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Registration success", "success");
FacesContext.getCurrentInstance().addMessage(null, msg);
System.out.println("befor redirect");
return "index?faces-redirect=true";
}
这是我的SQL脚本
CREATE DATABASE IF NOT EXISTS `myshop`;
USE `myshop`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`FIRST_NAME` varchar(45) DEFAULT NULL,
`LAST_NAME` varchar(45) DEFAULT NULL,
`EMAIL` varchar(45) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `address`;
CREATE TABLE `address` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`TOWN` varchar(45) DEFAULT NULL,
`DISTRICT` varchar(45) DEFAULT NULL,
`COUNTRY` varchar(45) DEFAULT NULL,
`USER_ID` int(11) NOT NULL,
PRIMARY KEY (`ID`),
KEY `FK_USER_idx` (`USER_ID`),
CONSTRAINT `FK_USER` FOREIGN KEY (`USER_ID`) REFERENCES `user` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
答案 0 :(得分:1)
对我来说,看起来你已经指明了你的关系错了。看看我的Q&amp; A here。你特别希望看到单向1到多个例子。根据您当前的配置,我认为Hibernate正在尝试创建2个单向1对多关系,这会导致问题。
在insertable = false, updateable = false, nullable = false
注释中指定@JoinColumn
无法做任何事情。如果您使用Hibernate根据注释创建数据库模式,那些修饰符只会执行某些操作。由于您已经拥有架构,因此它无法执行任何操作。
如果您可以发布registerUser
方法,则有助于调试目的。
public class Address implements Serializable {
....
@ManyToOne
@JoinColumn(name = "USER_ID")
private User user;
}
public class User implements Serializable{
....
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private Collection<Address> addresses;
}
答案 1 :(得分:1)
正如您所获得的异常java.sql.SQLException: Field 'USER_ID' doesn't have a default value
您可以使用@JoinColumn(name = "USER_ID", insertable=false, updatable=false, nullable=false), columnDefinition = “int default 100"
您可以提供任何值,尽管您想要成为默认值。
第二件事,可能是你想在制作外键时添加某个地方referencedColumnName = "SOME_COLUMN"
。
希望它有所帮助。
编辑:
请让我解释一下,referencedColumnName可用于指定另一列作为另一个表的默认id列。
Table1
id int identity
tableb_key varchar
Table2
id int identity
key varchar unique
// in class for Table1
@Joincolumn(column="tableb_key", referencedColumnName="key")
答案 2 :(得分:-1)
在执行OneToMany和ManyToOne时,您必须在Chilid对象中添加Parent对象以及在Parent Ojbect中添加Child 我的意思是你需要这样做 a.setUser(用户); b.setUser(用户);