gpt4 book ai didi

java - 外键没有默认值

转载 作者:行者123 更新时间:2023-11-29 03:00:36 26 4
gpt4 key购买 nike

这是我的 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)

如果我从 FORIGN_KEY“USER_ID”中删除“NOTNULL”约束,就不会出现错误但我需要将其设为 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;

最佳答案

因为您得到的异常是 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" 之类的注释为 Hibernate 中的列设置默认值

尽管您希望将 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")

关于java - 外键没有默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24277652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com