代码之家  ›  专栏  ›  技术社区  ›  dharga

休眠自动增量设置

  •  0
  • dharga  · 技术社区  · 14 年前

    如何为下表定义实体。我有些东西不管用,我只是想看看我该怎么做。

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

    我得到下面的圣。

    [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 回复  |  直到 14 年前
        1
  •  0
  •   rasata    14 年前

    您的问题似乎不是休眠问题:

    • 也许您已经为“seqno”定义了一个值,然后试图保存它

    • 或者在数据库的“设计”视图中,应确保将“seqno”列的“identity”属性设置为“yes”。

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

    设置标识“插入dbo.memberSelectedOptions” 论

        2
  •  0
  •   dharga    14 年前

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