加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – play framework Exception – field ..没有默认值

发布时间:2020-12-15 08:47:24 所属栏目:Java 来源:网络整理
导读:运行我的Play框架小应用程序我得到以下异常: … Caused by: java.sql.BatchUpdateException: Field ‘sentTO_id’ doesn’t have a default value … 错误播放在浏览器中吐出 PersistenceException occured : insert into Mail_User (Mail_id, sentBCC_id)
运行我的Play框架小应用程序我得到以下异常:

… Caused by: java.sql.BatchUpdateException: Field ‘sentTO_id’
doesn’t have a default value …

错误播放在浏览器中吐出

PersistenceException occured : insert into Mail_User (Mail_id,
sentBCC_id) values (?,?)

我有2个实体邮件和用户

1)Mail.java

@Entity
public class Mail extends Model {


   public String title;

   public Date sentAt;
   @OneToMany
   public List<User> sentTO;
   @OneToMany
   public List<User> sentBCC;


    public String content;

    @ManyToOne
    public User author;

//more code ...
}

2)User.java

@Entity
public class User extends Model {

    public String email;
    public String password;
    public String fullname;
    public boolean isAdmin;

    @OneToMany(mappedBy="author",cascade= CascadeType.ALL)
    public List<Mail> mails ;

    public User(String email,String password,String fullname) {
        this.email = email;
        this.password = password;
        this.fullname = fullname;
    }

}

全栈

Internal Server Error (500) for request POST /mails/create

Execution exception (In {module:crud}/app/controllers/CRUD.java around
line 152) PersistenceException occured : insert into Mail_User
(Mail_id,sentBCC_id) values (?,?)

play.exceptions.JavaExecutionException: insert into Mail_User
(Mail_id,?) at
play.mvc.ActionInvoker.invoke(ActionInvoker.java:231) at
Invocation.HTTP Request(Play!) Caused by:
javax.persistence.PersistenceException: insert into Mail_User
(Mail_id,?) at
play.db.jpa.JPABase._save(JPABase.java:38) at
controllers.CRUD.create(CRUD.java:152) at
play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:548)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:502) at
play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:478)
at
play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:473)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161) … 1 more
Caused by: javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: Could not execute JDBC
batch update at
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214)
at
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
at
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1153)
at
org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:798)
at play.db.jpa.JPABase._save(JPABase.java:35) … 7 more Caused by:
org.hibernate.exception.GenericJDBCException: Could not execute JDBC
batch update at
org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
at
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
at
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at
org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
at
org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
at
org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
at
org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1179)
at
org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273) at
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
at
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188)
at
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:345)
at
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) at
org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795)
… 8 more Caused by: java.sql.BatchUpdateException: Field
‘sentTO_id’ doesn’t have a default value at
com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2018)
at
com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449)
at
org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
… 20 more

我已经有一段时间以来一直在摸索这个错误,但无法解决这个问题.
有人能帮助我理解为什么我会收到此错误吗?

解决方法

首先,sendTo和sendBCC应映射为@ManyToMany.

其次,默认情况下,使用连接表实现非定向@OneToMany映射和所有@ManyToMany映射,并且连接表名称的默认生成策略不允许您在相同类之间具有多个此类关系.

您需要明确指定连接表名称:

@ManyToMany
@JoinTable(name = "User_Mail_To")
public List<User> sentTO;

@ManyToMany
@JoinTable(name = "User_Mail_Bcc")
public List<User> sentBCC;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读