mercredi 29 juin 2016

Saving an entity with composite key via JPA+Hibernate results in exception

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!

Aucun commentaire:

Enregistrer un commentaire