gpt4 book ai didi

java - Hibernate 不允许我添加具有自动生成身份 PK 的 Derby 数据库实体

转载 作者:行者123 更新时间:2023-12-01 12:30:44 25 4
gpt4 key购买 nike

我有一个包含以下字段的 Derby 数据库表。数据库ID字段自动生成并递增。

create table "Yaya".SERVICELOG
(
ID INTEGER default AUTOINCREMENT: start 1 increment 1 not null primary key,
TXTIME TIMESTAMP,
EVENTLEVEL INTEGER,
EVENTTYPE VARCHAR(255),
DESCRIPTION VARCHAR(4096)
)

使用 NetBeans 我正在尝试使用 Hibernate 作为 ORM。我以前使用过 Linq2SQL 和 EF...这变得很痛苦。

这是我生成的 POJO,我相信注释是正确的。

// Generated Sep 19, 2014 12:26:20 PM by Hibernate Tools 4.3.1


import java.util.Date;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
* Servicelog generated by hbm2java
*/
public class Servicelog implements java.io.Serializable {

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private Date txtime;
private Integer eventlevel;
private String eventtype;
private String description;

public Servicelog() {
}


public Servicelog(int id) {
this.id = id;
}
public Servicelog(int id, Date txtime, Integer eventlevel, String eventtype, String description) {
this.id = id;
this.txtime = txtime;
this.eventlevel = eventlevel;
this.eventtype = eventtype;
this.description = description;
}

public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}
public Date getTxtime() {
return this.txtime;
}

public void setTxtime(Date txtime) {
this.txtime = txtime;
}
public Integer getEventlevel() {
return this.eventlevel;
}

public void setEventlevel(Integer eventlevel) {
this.eventlevel = eventlevel;
}
public String getEventtype() {
return this.eventtype;
}

public void setEventtype(String eventtype) {
this.eventtype = eventtype;
}
public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}
}

这是我的插入代码。

    Session session = StartupBean.sessionFactory.openSession();
session.beginTransaction();
Servicelog sl = new Servicelog();
sl.setDescription("IN IT TO WIN IT");
session.save(sl);
session.getTransaction().commit();
session.close();

它在 glassfish 服务器控制台中的 session.getTransaction().commit();

Info: HHH000041: Configured SessionFactory: null WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!) Info: HHH000401: using driver [org.apache.derby.jdbc.ClientDriver] at URL [jdbc:derby://localhost:1527/yayadb] Info: HHH000046: Connection properties: {user=yayaadmin, password=****} Info:
HHH000006: Autocommit mode: false Info: HHH000115: Hibernate connection pool size: 20 (min=1) Info: HHH000400: Using dialect: org.hibernate.dialect.DerbyDialect WARN: HHH000430: The DerbyDialect dialect has been deprecated; use one of the version-specific dialects instead Info: HHH000399: Using default transaction strategy (direct JDBC transactions) Info: HHH000397: Using ASTQueryTranslatorFactory Info: Loading application [yaya] at [/yaya] Info: yaya was successfully deployed in 6,323 milliseconds.

WARN: SQL Error: -1, SQLState: 42Z23

ERROR: Attempt to modify an identity column 'ID'.

Warning: StandardWrapperValve[DbTest]: Servlet.service() for servlet DbTest threw exception org.hibernate.exception.SQLGrammarException: could not prepare statement at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:196) at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:96) at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.buildBatchStatement(AbstractBatchImpl.java:152) at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.getBatchStatement(AbstractBatchImpl.java:141) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3102) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3587) at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:103) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:453) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:421) at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177) at com.ya.ya.zzzyaya.DbTest.processRequest(DbTest.java:97) at com.ya.ya.zzzyaya.DbTest.doGet(DbTest.java:116) at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188) at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191) at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168) at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838) at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544) at java.lang.Thread.run(Thread.java:745)

Caused by: java.sql.SQLSyntaxErrorException: Attempt to modify an identity column 'ID'.

at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source) at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source) at org.apache.derby.client.am.Connection.prepareStatement(Unknown Source) at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:103) at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:186) ... 45 more Caused by: org.apache.derby.client.am.SqlException: Attempt to modify an identity column 'ID'. at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source) at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source) at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source) at org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source) at org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source) at org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source) at org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown Source) at org.apache.derby.client.am.PreparedStatement.readPrepareDescribeInputOutput(Unknown Source) at org.apache.derby.client.am.PreparedStatement.flowPrepareDescribeInputOutput(Unknown Source) at org.apache.derby.client.am.PreparedStatement.prepare(Unknown Source) at org.apache.derby.client.am.Connection.prepareStatementX(Unknown Source) ... 48 more

我已经研究了几个小时,但我不知道如何继续。我必须在 hibernate 中更改什么才能使其足够智能而不会输入主键?

最佳答案

好的,我明白了,部分感谢 R​​p- 的评论

private int id;

必须更改为

private Integer id; 

并在Servicelog.hbm.xml中

    <id name="id" type="int">
<column name="ID" />
<generator class="identity" />
</id>

    <id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="assigned" />
</id>

关于java - Hibernate 不允许我添加具有自动生成身份 PK 的 Derby 数据库实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25939024/

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