0

Here is my entity:

@Entity
@Table(name = "foo.bar")
public class Foobar implements Serializable {

    private static final long   serialVersionUID    = 1L;

    @Id
    @Column(name = "fk_ruleId")
    private Long                ruleId;

    @Id
    @Column(name = "fk_storageId")
    private Long                storageId;

    @Id
    @Column(name = "logicalDestination")
    private String              logicalDestination;

    public Long getRuleId() {
        return ruleId;
    }

    public void setRuleId(Long ruleId) {
        this.ruleId = ruleId;
    }

    public Long getStorageId() {
        return storageId;
    }

    public void setStorageId(Long storageId) {
        this.storageId = storageId;
    }

    public String getLogicalDestination() {
        return logicalDestination;
    }

    public void setLogicalDestination(String logicalDestination) {
        this.logicalDestination = logicalDestination;
    }

} // end class

And here is the appropriate create table statement (MS SQL):

CREATE TABLE [foo].[bar](
    [fk_ruleId] [numeric](19, 0) NOT NULL,
    [fk_storageId] [int] NOT NULL,
    [logicalDestination] [varchar](10) NOT NULL,
 CONSTRAINT [PK_foobar] PRIMARY KEY CLUSTERED 
(
    [fk_ruleId] ASC,
    [fk_storageId] ASC,
    [logicalDestination] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

I'm trying to save an entity with the following snippet:

Foobar newMapElement = new Foobar();
        newMapElement.setLogicalDestination("6");
        newMapElement.setStorageId(4l);
        newMapElement.setRuleId(3l);

        service.saveMap(newMapElement);

where service resolves to:

em.persist(newMapElement);

The mapping itseld seems to be fine. However, trying to save results is an exception:

com.microsoft.sqlserver.jdbc.SQLServerException: Index "4" is out of range.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:714)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:723)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1048)
    at org.jboss.jca.adapters.jdbc.CachedPreparedStatement.setString(CachedPreparedStatement.java:195)
    at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.setString(WrappedPreparedStatement.java:637)
    at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$1.doBind(VarcharTypeDescriptor.java:57) [hibernate-core-4.2.15.Final.jar:4.2.15.Final]
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:93) [hibernate-core-4.2.15.Final.jar:4.2.15.Final]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:284) [hibernate-core-4.2.15.Final.jar:4.2.15.Final]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:279) [hibernate-core-4.2.15.Final.jar:4.2.15.Final]
    at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:343) [hibernate-core-4.2.15.Final.jar:4.2.15.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrateId(AbstractEntityPersister.java:2835) [hibernate-core-4.2.15.Final.jar:4.2.15.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2804) [hibernate-core-4.2.15.Final.jar:4.2.15.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3076) [hibernate-core-4.2.15.Final.jar:4.2.15.Final]
    ... 109 more

Why is that??

Thanks in advance!

UPDATE:

I've enabled the sql logging and here is the result:

2016-06-27 09:42:20,528 DEBUG [jboss.jdbc.spy] (http-executor-threads - 37) java:/ResourcesDS [Connection] prepareStatement(insert into Foo.bar (fk_ruleId, logicalDestination, fk_storageId) values (?, ?, ?))
2016-06-27 09:42:20,528 DEBUG [jboss.jdbc.spy] (http-executor-threads - 37) java:/ResourcesDS [Connection] prepareStatement(insert into Foo.bar (fk_ruleId, logicalDestination, fk_storageId) values (?, ?, ?))
2016-06-27 09:42:20,528 DEBUG [jboss.jdbc.spy] (http-executor-threads - 37) java:/ResourcesDS [PreparedStatement] setLong(1, 1)
2016-06-27 09:42:20,528 DEBUG [jboss.jdbc.spy] (http-executor-threads - 37) java:/ResourcesDS [PreparedStatement] setLong(1, 1)
2016-06-27 09:42:20,528 DEBUG [jboss.jdbc.spy] (http-executor-threads - 37) java:/ResourcesDS [PreparedStatement] setString(2, 6)
2016-06-27 09:42:20,528 DEBUG [jboss.jdbc.spy] (http-executor-threads - 37) java:/ResourcesDS [PreparedStatement] setString(2, 6)
2016-06-27 09:42:20,528 DEBUG [jboss.jdbc.spy] (http-executor-threads - 37) java:/ResourcesDS [PreparedStatement] setLong(3, 1)
2016-06-27 09:42:20,528 DEBUG [jboss.jdbc.spy] (http-executor-threads - 37) java:/ResourcesDS [PreparedStatement] setLong(3, 1)
2016-06-27 09:42:20,528 DEBUG [jboss.jdbc.spy] (http-executor-threads - 37) java:/ResourcesDS [PreparedStatement] setLong(4, 4)
2016-06-27 09:42:20,528 DEBUG [jboss.jdbc.spy] (http-executor-threads - 37) java:/ResourcesDS [PreparedStatement] setLong(4, 4)

the "4" is not the String parameter, it's the index in the prepared statement, what I don't understand - where does it come from??

  • Don't you need @IdClass annotation? – K.Nicholas Jun 26 '16 at 18:43
  • you are right, Ive added the idClass annotation and the corresponding id class, unfortunately, still the same error msg... – user1124372 Jun 26 '16 at 19:41
  • I looks a little mismatched. The database is calling storageId an int, but you have it declared as a Long. The stacktrace seems to be trying to convert a String for some reason. On top of that you are using Spring. Finally, you have storageId as a foreign key, but I see no other Entity defined. Perhaps see [How to map compound primary key in JPA, where part of Primary Key is a Foreign Key](http://stackoverflow.com/questions/36338527/how-to-map-compound-primary-key-in-jpa-where-part-of-primary-key-is-a-foreign-k/36342988#36342988) for general advice on dealing with composite PK's. – K.Nicholas Jun 26 '16 at 20:40
  • int vs long is not a problem, already tried fixing it as well as fk_storage, there is no foreign key defined on it, it's just the misleading name (it wasn't me who designed the database). Just edited my question and added some sql logging, weird – user1124372 Jun 27 '16 at 07:49

0 Answers0