当前位置: 首页 > 知识库问答 >
问题:

外键没有默认值

尉迟宪
2023-03-14

这是我的用户。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;
}

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

错误不会来,如果我删除"NOTNULL"约束从FORIGN_KEY"USER_ID",但我需要使它作为一个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;

共有3个答案

戚弘和
2023-03-14

在执行OneToMany和ManyToOne时,您必须在Chilid对象中添加父对象,以及在父对象中添加子对象,我的意思是您需要执行一个.setUser(user);b、 setUser(用户);

俞博涛
2023-03-14

在我看来,你把你们的关系说错了。看看我的Q

@JoinColumn注释上指定insertable=false、updateable=false、nullable=false不会起任何作用。只有在使用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;
}
宰子琪
2023-03-14

因为你得到的异常是java.sql.SQLExc0019:字段'USER_ID'没有默认值您可以在Hibernate中使用注释设置您的列的默认值,如@JoinColVIII(name="USER_ID",可插入=false,可更新=false, nullable=false),列定义="int默认100"

您可以给出任何值,不管您希望默认值是多少。

第二件事,可能是在制作外键时,您希望在某处添加referencedColumnName=“SOME\u COLUMN”

希望有帮助。

编辑:

请让我解释一下,引用的列名称可以用来指定另一个列作为另一个表的默认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")
 类似资料:
  • 我正在为Django管理员设置: model.py 在admin.py 对此,我得到了以下错误: (admin. E202)“磁盘。文件”没有“磁盘。设置”的外键 根据https://docs . django project . com/en/dev/ref/models/fields/# django . db . models . foreign key中的文档,它应该可以工作。 我做了一些

  • 我不明白为什么我应用程序会删除异常“字段'ID_Employeer'没有默认值”。 是我的主键,当我试图将任何信息保存到我的数据库时,我得到了这个异常,但是,当我只是试图从DB读取信息时,它是正常的。 我读了关于StackOverflow的主题,但我仍然不明白我应用程序中的答案。我正在使用Hibernate5.2.11。 我的: 我的冬眠: 下一个CharacterInfo类 主类 最后例外

  • 问题内容: 我有一个表,其中父对象具有可选的多对一关系。问题在于该表已设置为默认将fkey列设置为0。 选择时,使用fetch =“ join”等-fkey的默认值0被用来反复尝试从另一个表中选择ID0。当然这不存在,但是我怎么能告诉Hibernate将0的值与NULL相同- 在获取不存在的关系时不循环20多次以上? 问题答案: 我能够通过创建一个id- long类型来解决此问题,该类型扩展了内置

  • 问题内容: 我面对的是Hibernate遇到的一个简单问题,但无法解决(无法访问的Hibernate论坛当然无济于事)。 我有一个简单的课程想继续学习,但是请继续学习: 持久化类的相关代码为: 实际的运行代码很简单: 我在注解中尝试了一些“策略”,但似乎不起作用。初始化也无济于事!(例如)。 谁能给我一些启示? 编辑2: 确认:不能解决问题 求助: 重新创建数据库解决了问题 问题答案: 有时,即使

  • 问题内容: 我收到一条错误消息: 每个人都可以帮助我吗?我的数据库字段不为空。但我想得到这个结果: 并且 两者都成功(在客户端中都是成功,但是在Java代码中是error,错误代码位于标题的顶部),而不是false;我的mysql jar是mysql-connector- java-5.0.8 问题答案: 该错误是不言自明的。您的列没有默认值。因此,在插入期间,mysql无法找出要在列中插入的内容

  • 我被一个奇怪的编程问题困住了。我已经定义了一个模式,其中如果没有为列(列名是Title)传递值,那么将使用默认值(Harry)。 标题|字符变化(64)|默认“Harry”::字符变化 而且,我正在尝试通过我的java应用程序使用hibernate插入条目。 发生的情况是,hibernate在任何查询中都显式地为该列提供空值,因此这些条目的默认值不会显示在表中。 这里要提到的是,当我自己执行任何i