开发者

Hibernate Auto-Increment Setup

How do I define an entity for the following table. I've got something that isn't working and I just want to see what I'm supposed to do.

USE [BAMPI_TP_dev]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MemberSelectedOptions](
    [OptionId] [int] NOT NULL,
[SeqNo开发者_如何学运维] [smallint] IDENTITY(1,1) NOT NULL,
[OptionStatusCd] [char](1) NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

This is what I have already that isn't working.

@Entity
@Table(schema="dbo", name="MemberSelectedOptions")
public class MemberSelectedOption extends BampiEntity implements Serializable {

    @Embeddable
    public static class MSOPK implements Serializable {
        private static final long serialVersionUID = 1L;

        @Column(name="OptionId")
        int optionId;

        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name="SeqNo", unique=true, nullable=false)
        BigDecimal seqNo;

        //Getters and setters here...

}

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    MSOPK pk = new MSOPK();

    @Column(name="OptionStatusCd")
    String optionStatusCd;

    //More Getters and setters here...
}

I get the following ST.

[5/25/10 15:49:40:221 EDT] 0000003d JDBCException E org.slf4j.impl.JCLLoggerAdapter error Cannot insert explicit value for identity column in table 'MemberSelectedOptions' when IDENTITY_INSERT is set to OFF.
[5/25/10 15:49:40:221 EDT] 0000003d AbstractFlush E org.slf4j.impl.JCLLoggerAdapter error Could not synchronize database state with session
                             org.hibernate.exception.SQLGrammarException: could not insert: [com.bob.proj.ws.model.MemberSelectedOption]
                             at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
                             at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
                             at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2285)
                             at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2678)
                             at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
                             at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
                             at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
                             at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
                             at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
                             at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
                             at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
                             at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
                             at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
                             at com.bcbst.bamp.ws.dao.MemberSelectedOptionDAOImpl.saveMemberSelectedOption(MemberSelectedOptionDAOImpl.java:143)
                             at com.bcbst.bamp.ws.common.AlertReminder.saveMemberSelectedOptions(AlertReminder.java:76)
                             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                             at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)


It appears that your problem is not an hibernate problems:

  • Perhaps you have defined a value for "SeqNo" and then tried to saved it

  • Or in the design view of your database, you should ensure that you set the "IDENTITY" property to "yes" for the column "SeqNo"

And you should do so by executing this after your code :

set IDENTITY_INSERT dbo.MemberSelectedOptions ON


You can't use generators on composite keys

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜