休眠自动增量设置

时间:2010-05-25 20:16:52

标签: java sql-server-2005 hibernate

如何为下表定义实体。我有一些不起作用的东西,我只想看看我应该做些什么。

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

这就是我已经没有用的。

@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...
}

我得到以下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)

2 个答案:

答案 0 :(得分:0)

看来您的问题不是休眠问题:

  • 也许您为“SeqNo”定义了一个值,然后尝试保存它

  • 或者在数据库的设计视图中,您应该确保为“SeqNo”列设置“IDENTITY”属性为“是”

您应该在代码之后执行此操作:

  

设置IDENTITY_INSERT dbo.MemberSelectedOptions   ON

答案 1 :(得分:0)

您不能在复合键上使用生成器