7

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)

1
  • Putting @Transient before @ManyToOne worked fine for me. Dec 7, 2017 at 11:55

4 Answers 4

7

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

5
  • This approach I can't work out due to some business reasons. So, in the second table the Identity column of first table and another column would be a composite primary key. The identity column in the second table would be a foreign key dependent on the first table.
    – swateek
    Mar 26, 2015 at 16:36
  • Mark it as read-only. But in general, my answer is THE answer. Two mappings of one column cannot be used as writeable... Hope it helps Mar 26, 2015 at 16:49
  • Now..with some changes in the entity class.. (I have done that read only too) this error isn't coming. But something else is coming up, I am confused if I should edit this question or start a different thread? Help please.
    – swateek
    Mar 26, 2015 at 16:54
  • 1
    To get more attention,ask new question. This is down in the stack. Mar 26, 2015 at 17:17
  • Here's the new question that I have posted, there are a few changes in the entity classes too. If you could please help. stackoverflow.com/questions/29285225/…
    – swateek
    Mar 26, 2015 at 17:38
4

In my case issue was with user_type field. It was defined as discriminator and as public property. It wasn't marked with @Column but somehow Hibernate anyway failed on this

@Entity
@Table(
    name = "abstract_users",
    indexes = {@Index(name = "idx__abstract_users__is_enabled", columnList = "is_enabled")}
)
@Access(AccessType.FIELD)
@Inheritance(strategy = InheritanceType.JOINED)
// @TODO: rename user_type to discriminator
@DiscriminatorColumn(name = "user_type", discriminatorType = DiscriminatorType.STRING, length = 10)
abstract public class AbstractUser extends CreateUpdateTimestampableBase
{

    // @TODO: rename user_type to discriminator
    public String user_type;

    @Min(0)
    @NotNull
    @Column(precision = 19, scale = 2)
    protected BigDecimal balance;

    //...
}
3
  • 1
    Hibernate picks up any property that is not marked transient or annotated with @Transient. That is why user_type detected as a database column. Feb 17, 2017 at 0:14
  • Can we say that Hibernate does not allow you to use the discriminator column as a property of the class? Oct 8, 2019 at 12:01
  • Helped me, thanks
    – breakline
    Dec 7, 2021 at 15:48
4

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;
0
0

In your entity, replace

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

by

@ManyToOne
@JoinColumn(name="salt_id", updatable = false, insertable = false)
private SaltTranDef saltTranDef;

it worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.