gpt4 book ai didi

java - JPA/Hibernate : java. sql.SQLException:JDBC 调用中的参数无效:参数索引超出范围

转载 作者:行者123 更新时间:2023-12-01 17:34:26 29 4
gpt4 key购买 nike

我正在使用 Hibernate 4,并在另外两个表之间使用一个简单的“连接表”。以下是 JPA 1.0 兼容实体:

@Entity
@Table(name = "Companies")
public class Company
{
@Id
@Column
private Integer id;

@Column
private String name;

...
}

@Entity
@Table(name = "PQs")
public class PQ implements Serializable
{
@Id
@Column
private Integer id;

@Column
private String name;

...
}

如您所见,有两个相似的表,只是一个简单的 ID 加名称。现在是互连实体:

@Entity
@Table(name = "Partnerships")
@IdClass(value = PartnershipId.class)
public class Partnership implements Serializable
{
@Id
@Column(name = "pq_id", insertable = false, updatable = false)
private Integer pqId;

@Id
@Column(name = "company_id", insertable = false, updatable = false)
private Integer companyId;

@Column(name = "ordinal_nbr")
private Integer ordinalNbr;

@ManyToOne
@JoinColumn(name = "pq_id", referencedColumnName = "id")
private PQ pq;

@ManyToOne
@JoinColumn(name = "company_id", referencedColumnName = "id")
private Company company;

...
}

请注意此处 @Column 上的 ...、insertable = false、updatable = false

public class PartnershipId implements Serializable
{
private Integer pqId;

private Integer companyId;

public PartnershipId()
{
}

public PartnershipId(Integer pqId, Integer companyId)
{
this.pqId = pqId;
this.companyId = companyId;
}

...
}

运行以下测试代码时

public class Main
{
private static String PERSISTENCE_UNIT_NAME = "standalonePu";

private static EntityManagerFactory emf;
private static EntityManager em;
private static EntityTransaction trans;

public static void main(String[] args)
{
setUp(PERSISTENCE_UNIT_NAME);
trans.begin();

PQ detachedPq = new PQ(1, "Test PQ");
Company detachedCompany = new Company(1, "Test Company");

PQ pq = em.merge(detachedPq);
Company company = em.merge(detachedCompany);

Partnership detachedPartnership = new Partnership(1, 1, 1);
detachedPartnership.setPQ(pq);
detachedPartnership.setCompany(company);

Partnership partnership = em.merge(detachedPartnership);

partnership = em.find(Partnership.class, new PartnershipId(1, 1));

System.out.println("Persistent partnership = ("
+ partnership.getPQId() + ", "
+ partnership.getCompanyId() + ", "
+ partnership.getOrdinalNbr() + ")");

trans.commit();
close();
}

private static void setUp(String puName)
{
emf = Persistence.createEntityManagerFactory(puName);
em = emf.createEntityManager();
trans = em.getTransaction();
}

private static void close()
{
em.close();
emf.close();
}
}

...Hibernate 因 trans.commit(); 上出现非常奇怪的异常而失败:

ERROR: Invalid argument in JDBC call: parameter index out of range: 4
Exception in thread "main" javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:90)
at main.Main.main(Main.java:44)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Invalid argument in JDBC call: parameter index out of range: 4
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1347)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1280)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:78)
... 1 more
Caused by: org.hibernate.exception.GenericJDBCException: Invalid argument in JDBC call: parameter index out of range: 4
at org.hibernate.exception.internal.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:148)
at org.hibernate.exception.internal.SQLStateConverter.convert(SQLStateConverter.java:136)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:124)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:131)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:80)
at $Proxy12.setInt(Unknown Source)
at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$1.doBind(IntegerTypeDescriptor.java:57)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:82)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:305)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:300)
at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:358)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2599)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3276)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:80)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:186)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:344)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1084)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:319)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:100)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:173)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:73)
... 1 more
Caused by: java.sql.SQLException: Invalid argument in JDBC call: parameter index out of range: 4
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.outOfRangeArgument(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.checkSetParameterIndex(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.setInt(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:124)
... 22 more
Caused by: org.hsqldb.HsqlException: Invalid argument in JDBC call: parameter index out of range: 4
at org.hsqldb.error.Error.error(Unknown Source)
... 31 more

测试的配置是Hibernate 4.0.0.CR4和HSQLDB 2.0。我还使用 MySQL 5 进行了测试,并出现非常相同 JDBC 异常,因此显然 Hibernate 生成了错误的代码。

解决此问题的解决方法是将 ..., insertable = false, updatable = false 放到关系的 @JoinColumn 上,但这并不是我真正想要的想要。

那么,这是一个 Hibernate 错误吗?上面的代码从JPA的角度来看应该是正确的...

最佳答案

这是一个错误:

https://hibernate.onjira.com/browse/JPA-24

我使用 JPA 引用实现 EclipseLink 2.3 对此进行了测试,它运行良好。

关于java - JPA/Hibernate : java. sql.SQLException:JDBC 调用中的参数无效:参数索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7875199/

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