代码(实体类查看最下方)
@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;
}
}