Hibernate 使用注解生成一对多的数据结构,进行数据存储时报错

代码(实体类查看最下方)

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Session session = HibernateUtils.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        CustomerEntity customerEntity = new CustomerEntity();
        customerEntity.setCustName("蒋大卫");

        LinkmanEntity linkmanEntity1 = new LinkmanEntity();
        linkmanEntity1.setLkmName("李秘书");
        LinkmanEntity linkmanEntity2 = new LinkmanEntity();
        linkmanEntity2.setLkmId(2);
       linkmanEntity2.setLkmName("王助理");
        customerEntity.getCstLinkmenByCustId().add(linkmanEntity1);
        customerEntity.getCstLinkmenByCustId().add(linkmanEntity2);
        linkmanEntity1.setCstCustomerByLkmCustId(customerEntity);
        linkmanEntity2.setCstCustomerByLkmCustId(customerEntity);
        session.save(customerEntity);
        //session.save(linkmanEntity1);
        session.save(linkmanEntity2);
        transaction.commit();
    }

异常

HTTP Status 500 – Internal Server Error

Type Exception Report

Message error performing isolated work

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.hibernate.exception.SQLGrammarException: error performing isolated work
    org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
    org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
    org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:79)
    org.hibernate.id.enhanced.TableStructure$1.getNextValue(TableStructure.java:125)
    org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
    org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:452)
    org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:691)
    org.hibernate.internal.SessionImpl.save(SessionImpl.java:683)
    org.hibernate.internal.SessionImpl.save(SessionImpl.java:678)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:355)
    com.sun.proxy.$Proxy33.save(Unknown Source)
    com.hb.demo.web.DemoServlet.doPost(DemoServlet.java:45)
    com.hb.demo.web.DemoServlet.doGet(DemoServlet.java:53)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    com.hb.demo.web.filter.GenericEncodingFilter.doFilter(GenericEncodingFilter.java:31)
Root Cause

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'user.hibernate_sequence' doesn't exist
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    com.mysql.jdbc.Util.getInstance(Util.java:408)
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
    com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
    com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
    com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487)
    com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
    com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966)
    org.hibernate.id.enhanced.TableStructure.executeQuery(TableStructure.java:216)
    org.hibernate.id.enhanced.TableStructure.access$300(TableStructure.java:46)
    org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:138)
    org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:126)
    org.hibernate.jdbc.WorkExecutor.executeReturningWork(WorkExecutor.java:55)
    org.hibernate.jdbc.AbstractReturningWork.accept(AbstractReturningWork.java:34)
    org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:57)
    org.hibernate.id.enhanced.TableStructure$1.getNextValue(TableStructure.java:125)
    org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
    org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:452)
    org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:691)
    org.hibernate.internal.SessionImpl.save(SessionImpl.java:683)
    org.hibernate.internal.SessionImpl.save(SessionImpl.java:678)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:355)
    com.sun.proxy.$Proxy33.save(Unknown Source)
    com.hb.demo.web.DemoServlet.doPost(DemoServlet.java:45)
    com.hb.demo.web.DemoServlet.doGet(DemoServlet.java:53)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    com.hb.demo.web.filter.GenericEncodingFilter.doFilter(GenericEncodingFilter.java:31)
Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/9.0.1

解决
hibernate.cfg.xml中添加

<property name="hibernate.id.new_generator_mappings">false</property>

参考
https://stackoverflow.com/questions/32968527/hibernate-sequence-doesnt-exist
CustomerEntity 与 LinkmanEntity 1对n

@Entity
@Table(name = "cst_customer", schema = "user")
public class CustomerEntity {
    private long custId;
    private String custName;
    private Long custUserId;
    private Long custCreateId;
    private String custSource;
    private String custIndustry;
    private String custLevel;
    private String custLinkman;
    private String custPhone;
    private String custMobile;
    private Set cstLinkmenByCustId = new HashSet<LinkmanEntity>();

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "cust_id")
    public long getCustId() {
        return custId;
    }

    public void setCustId(long custId) {
        this.custId = custId;
    }

    @Basic
    @Column(name = "cust_name")
    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    @Basic
    @Column(name = "cust_user_id")
    public Long getCustUserId() {
        return custUserId;
    }

    public void setCustUserId(Long custUserId) {
        this.custUserId = custUserId;
    }

    @Basic
    @Column(name = "cust_create_id")
    public Long getCustCreateId() {
        return custCreateId;
    }

    public void setCustCreateId(Long custCreateId) {
        this.custCreateId = custCreateId;
    }

    @Basic
    @Column(name = "cust_source")
    public String getCustSource() {
        return custSource;
    }

    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }

    @Basic
    @Column(name = "cust_industry")
    public String getCustIndustry() {
        return custIndustry;
    }

    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }

    @Basic
    @Column(name = "cust_level")
    public String getCustLevel() {
        return custLevel;
    }

    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }

    @Basic
    @Column(name = "cust_linkman")
    public String getCustLinkman() {
        return custLinkman;
    }

    public void setCustLinkman(String custLinkman) {
        this.custLinkman = custLinkman;
    }

    @Basic
    @Column(name = "cust_phone")
    public String getCustPhone() {
        return custPhone;
    }

    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }

    @Basic
    @Column(name = "cust_mobile")
    public String getCustMobile() {
        return custMobile;
    }

    public void setCustMobile(String custMobile) {
        this.custMobile = custMobile;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        CustomerEntity that = (CustomerEntity) o;

        if (custId != that.custId) {
            return false;
        }
        if (custName != null ? !custName.equals(that.custName) : that.custName != null) {
            return false;
        }
        if (custUserId != null ? !custUserId.equals(that.custUserId) : that.custUserId != null) {
            return false;
        }
        if (custCreateId != null ? !custCreateId.equals(that.custCreateId) : that.custCreateId != null) {
            return false;
        }
        if (custSource != null ? !custSource.equals(that.custSource) : that.custSource != null) {
            return false;
        }
        if (custIndustry != null ? !custIndustry.equals(that.custIndustry) : that.custIndustry != null) {
            return false;
        }
        if (custLevel != null ? !custLevel.equals(that.custLevel) : that.custLevel != null) {
            return false;
        }
        if (custLinkman != null ? !custLinkman.equals(that.custLinkman) : that.custLinkman != null) {
            return false;
        }
        if (custPhone != null ? !custPhone.equals(that.custPhone) : that.custPhone != null) {
            return false;
        }
        if (custMobile != null ? !custMobile.equals(that.custMobile) : that.custMobile != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (custId ^ (custId >>> 32));
        result = 31 * result + (custName != null ? custName.hashCode() : 0);
        result = 31 * result + (custUserId != null ? custUserId.hashCode() : 0);
        result = 31 * result + (custCreateId != null ? custCreateId.hashCode() : 0);
        result = 31 * result + (custSource != null ? custSource.hashCode() : 0);
        result = 31 * result + (custIndustry != null ? custIndustry.hashCode() : 0);
        result = 31 * result + (custLevel != null ? custLevel.hashCode() : 0);
        result = 31 * result + (custLinkman != null ? custLinkman.hashCode() : 0);
        result = 31 * result + (custPhone != null ? custPhone.hashCode() : 0);
        result = 31 * result + (custMobile != null ? custMobile.hashCode() : 0);
        return result;
    }

    @OneToMany(mappedBy = "cstCustomerByLkmCustId",cascade={CascadeType.ALL})
    public Set<LinkmanEntity> getCstLinkmenByCustId() {
        return cstLinkmenByCustId;
    }

    public void setCstLinkmenByCustId(Set<LinkmanEntity> cstLinkmenByCustId) {
        this.cstLinkmenByCustId = cstLinkmenByCustId;
    }
}
@Entity
@Table(name = "cst_linkman", schema = "user")
public class LinkmanEntity {
    private long lkmId;
    private String lkmName;
    private String lkmGender;
    private String lkmPhone;
    private String lkmMobile;
    private String lkmEmail;
    private String lkmQq;
    private String lkmPosition;
    private String lkmMemo;
    private CustomerEntity cstCustomerByLkmCustId;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "lkm_id")
    public long getLkmId() {
        return lkmId;
    }

    public void setLkmId(long lkmId) {
        this.lkmId = lkmId;
    }

    @Basic
    @Column(name = "lkm_name")
    public String getLkmName() {
        return lkmName;
    }

    public void setLkmName(String lkmName) {
        this.lkmName = lkmName;
    }

    @Basic
    @Column(name = "lkm_gender")
    public String getLkmGender() {
        return lkmGender;
    }

    public void setLkmGender(String lkmGender) {
        this.lkmGender = lkmGender;
    }

    @Basic
    @Column(name = "lkm_phone")
    public String getLkmPhone() {
        return lkmPhone;
    }

    public void setLkmPhone(String lkmPhone) {
        this.lkmPhone = lkmPhone;
    }

    @Basic
    @Column(name = "lkm_mobile")
    public String getLkmMobile() {
        return lkmMobile;
    }

    public void setLkmMobile(String lkmMobile) {
        this.lkmMobile = lkmMobile;
    }

    @Basic
    @Column(name = "lkm_email")
    public String getLkmEmail() {
        return lkmEmail;
    }

    public void setLkmEmail(String lkmEmail) {
        this.lkmEmail = lkmEmail;
    }

    @Basic
    @Column(name = "lkm_qq")
    public String getLkmQq() {
        return lkmQq;
    }

    public void setLkmQq(String lkmQq) {
        this.lkmQq = lkmQq;
    }

    @Basic
    @Column(name = "lkm_position")
    public String getLkmPosition() {
        return lkmPosition;
    }

    public void setLkmPosition(String lkmPosition) {
        this.lkmPosition = lkmPosition;
    }

    @Basic
    @Column(name = "lkm_memo")
    public String getLkmMemo() {
        return lkmMemo;
    }

    public void setLkmMemo(String lkmMemo) {
        this.lkmMemo = lkmMemo;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        LinkmanEntity that = (LinkmanEntity) o;

        if (lkmId != that.lkmId) {
            return false;
        }
        if (lkmName != null ? !lkmName.equals(that.lkmName) : that.lkmName != null) {
            return false;
        }
        if (lkmGender != null ? !lkmGender.equals(that.lkmGender) : that.lkmGender != null) {
            return false;
        }
        if (lkmPhone != null ? !lkmPhone.equals(that.lkmPhone) : that.lkmPhone != null) {
            return false;
        }
        if (lkmMobile != null ? !lkmMobile.equals(that.lkmMobile) : that.lkmMobile != null) {
            return false;
        }
        if (lkmEmail != null ? !lkmEmail.equals(that.lkmEmail) : that.lkmEmail != null) {
            return false;
        }
        if (lkmQq != null ? !lkmQq.equals(that.lkmQq) : that.lkmQq != null) {
            return false;
        }
        if (lkmPosition != null ? !lkmPosition.equals(that.lkmPosition) : that.lkmPosition != null) {
            return false;
        }
        if (lkmMemo != null ? !lkmMemo.equals(that.lkmMemo) : that.lkmMemo != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (lkmId ^ (lkmId >>> 32));
        result = 31 * result + (lkmName != null ? lkmName.hashCode() : 0);
        result = 31 * result + (lkmGender != null ? lkmGender.hashCode() : 0);
        result = 31 * result + (lkmPhone != null ? lkmPhone.hashCode() : 0);
        result = 31 * result + (lkmMobile != null ? lkmMobile.hashCode() : 0);
        result = 31 * result + (lkmEmail != null ? lkmEmail.hashCode() : 0);
        result = 31 * result + (lkmQq != null ? lkmQq.hashCode() : 0);
        result = 31 * result + (lkmPosition != null ? lkmPosition.hashCode() : 0);
        result = 31 * result + (lkmMemo != null ? lkmMemo.hashCode() : 0);
        return result;
    }

    @ManyToOne
    @JoinColumn(name = "lkm_cust_id", referencedColumnName = "cust_id", nullable = false)
    public CustomerEntity getCstCustomerByLkmCustId() {
        return cstCustomerByLkmCustId;
    }

    public void setCstCustomerByLkmCustId(CustomerEntity cstCustomerByLkmCustId) {
        this.cstCustomerByLkmCustId = cstCustomerByLkmCustId;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,012评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,628评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,653评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,485评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,574评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,590评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,596评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,340评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,794评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,102评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,276评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,940评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,583评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,201评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,441评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,173评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,136评论 2 352

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,617评论 18 399
  • Hibernate中配置主要分为两种:一种包含了Hibernate与数据库的基本连接信息,在Hibernate工作...
    FTOLsXD阅读 2,037评论 0 10
  • 一. Java基础部分.................................................
    wy_sure阅读 3,810评论 0 11
  • Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库...
    兰缘小妖阅读 1,200评论 1 18
  • Hibernate: 一个持久化框架 一个ORM框架 加载:根据特定的OID,把一个对象从数据库加载到内存中OID...
    JHMichael阅读 1,965评论 0 27