Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: Parameter index out of range (8 > number of parameters, which is 7)

Tags:

java

hibernate

Not a duplicate of this question Parameter index out of range (8 > number of parameters, which is 7)

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;

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;

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;

When inserting from a 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();
}

I get an error saying:

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)

like image 803
swateek Avatar asked Mar 25 '15 03:03

swateek


Video Answer


2 Answers

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":

the SaltTranUser entity class:

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

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

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

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

like image 162
Radim Köhler Avatar answered Sep 19 '22 02:09

Radim Köhler


I faced same issue when I was using the one-directional mapping (parent class containing child class, but child class doesn't keep the reference of parent class). The mapping looked like

@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name="jobcard_id", nullable=false)
private List<JobServiceMapping> services;

I got the error Parameter index out of range. Then I changed the annotations a bit and now it is working for me.

@OneToMany(mappedBy="jobcardId", cascade = CascadeType.ALL, fetch=FetchType.EAGER, orphanRemoval = true)
private List<JobServiceMapping> services;
like image 39
Pankaj Shinde Avatar answered Sep 19 '22 02:09

Pankaj Shinde