休眠:参数索引超出范围(8>参数数量,即7) [英] Hibernate: Parameter index out of range (8 > number of parameters, which is 7)

查看:164
本文介绍了休眠:参数索引超出范围(8>参数数量,即7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是该问题的重复参数索引超出范围(8个参数数量,即7)

我的SaltTranDef实体类是

My SaltTranDef entity class is

@Id
@Column(name="salt_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer saltId;

@Column(name="tran_type")
private String transactionType;

@Column(name="user_id")
private String userId;

@Column(name="parent_system")
private String parentSystem;

@Column(name="parent_sys_ref_id")
private String parentSystemReference;

@Column(name="status")
private int status;

@OneToMany(mappedBy = "saltTranDef")
@Cascade({ org.hibernate.annotations.CascadeType.ALL,
         org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
private Set<SaltTranUser> saltTranUsers;

SaltTranUser实体类是

And the SaltTranUser entity class is

@Id
@Column(name="salt_id")
private Integer saltId;

@Id
@Column(name="salt_property")
private String saltProp;

@Column(name="salt_value")
private String saltValue;

@ManyToOne
@JoinColumn(name="salt_id")
private SaltTranDef saltTranDef;

以上两个实体类都扩展了一个appeddSuperclass

Both the above entity classes extend a mappedSuperclass

@Column(name="cre_suid")
private String creatorId;

@Column(name="mod_suid")
private String modifiedId;

@Column(name="cre_date")
private Timestamp creationDate;

@Column(name="mod_date")
private Timestamp modificationDate;

从JUnit插入时:

@Test
public void testInsert(){

    SaltTranDef std = new SaltTranDef();
    SaltTranUser stu1 = new SaltTranUser();
    SaltTranUser stu2 = new SaltTranUser();
    SaltTranUser stu3 = new SaltTranUser();
    Set<SaltTranUser> set1 = new HashSet<SaltTranUser>();

    Transaction tx = session.beginTransaction();

    std.setParentSystem("A");
    std.setParentSystemReference("123");
    std.setStatus(10);
    std.setTransactionType("A");
    std.setUserId("1234");
    std.setCreationDate(new Timestamp(new Date().getTime()));
    std.setCreatorId("1234");

    session.persist(std);
//  session.flush();

    stu1.setCreationDate(new Timestamp(new Date().getTime()));
    stu1.setCreatorId("1234");
    stu1.setSaltProp("Fname");
    stu1.setSaltValue("Swateek");
    stu1.setSaltId(std.getSaltId());

    stu2.setCreationDate(new Timestamp(new Date().getTime()));
    stu2.setCreatorId("1234");
    stu2.setSaltProp("Lname");
    stu2.setSaltValue("Jena");
    stu2.setSaltId(std.getSaltId());

    stu3.setCreationDate(new Timestamp(new Date().getTime()));
    stu3.setCreatorId("1234");
    stu3.setSaltProp("Phone");
    stu3.setSaltValue("9900056668");
    stu3.setSaltId(std.getSaltId());

    set1.add(stu1);
    set1.add(stu2);
    set1.add(stu3);

    std.setSaltTranUsers(set1);

    session.save(std);
    tx.commit();
}

我收到一条错误消息:

严重:参数索引超出范围(8>参数数,即7). 2015年3月25日,上午8:06:35 org.hibernate.event.def.AbstractFlushingEventListener performExecutions 严重:无法将数据库状态与会话同步 org.hibernate.exception.GenericJDBCException:无法插入:[com.salt.entity.SaltTranUser] 在org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) 由以下原因引起:java.sql.SQLException:参数索引超出范围(8>参数数量,即7). 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)

SEVERE: Parameter index out of range (8 > number of parameters, which is 7). Mar 25, 2015 8:06:35 AM org.hibernate.event.def.AbstractFlushingEventListener performExecutions SEVERE: Could not synchronize database state with session org.hibernate.exception.GenericJDBCException: could not insert: [com.salt.entity.SaltTranUser] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) Caused by: java.sql.SQLException: Parameter index out of range (8 > number of parameters, which is 7). at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)

推荐答案

这种问题几乎总是与双列映射有关.的确如此.我们可以看到,此映射两次使用了一次列 "salt_id" :

This kind of problem is almost always related to double column mapping. And indeed. We can see, that this mapping uses one column twice "salt_id":

SaltTranUser实体类:

@Id
@Column(name="salt_id")
private Integer saltId;
...

@ManyToOne
@JoinColumn(name="salt_id")
private SaltTranDef saltTranDef;

那是错误的.最后,Hibernate会在一列中插入两次,即在INSERT,UPDATE中添加更多的参数,然后再插入列

And that is wrong. Hibernate is at the end inserting into one column twice, i.e. more arguments then columns in INSERT, UPDATE

这里的解决方案很可能非常简单-因为@ManyToOne似乎是错误的.我希望有一些特殊的列可供参考,例如: SaltTranDef_id

Solution here would be mostlikely very simple - because the @ManyToOne seems to be wrong. I would expect some special column for reference like: SaltTranDef_id

这篇关于休眠:参数索引超出范围(8>参数数量,即7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆