auto creation of sequence using hibernate tool

后端 未结 2 1771
[愿得一人]
[愿得一人] 2021-02-14 10:58

I wanted to to generate sequence using hibernate tool ( pojo to sql). And definitely it works fine.

@GeneratedValue(strategy = GenerationType.SEQUENCE, generato         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-14 11:06

    I looked it up in the Hibernate sources (4.2.7). It is not possible to specify this with an annotation (neither JPA nor Hibernate).

    However you can provide your own Dialect to achieve this.

    public class MyOwnOracleDialect extends Oracle10gDialect {
    
        @Override
        protected String getCreateSequenceString(final String sequenceName, final int initialValue, final int incrementSize)
            throws MappingException {
            String createSequenceString = super.getCreateSequenceString(sequenceName, initialValue, incrementSize);
            return createSequenceString + " NOCACHE ORDER CYCLE"; // modify this string as you like
        }
    }
    

    Have an entity like this

    @Entity
    public class MyEntity {
    
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqid-gen")
        @SequenceGenerator(name = "seqid-gen", sequenceName = "RTDS_ADSINPUT_SEQ", allocationSize = 1, initialValue = 0)
        @Column(name="id")
        private Long id;
    
        // ...
    
    }
    

    You can set your new Dialect as described in the Hibernate doc (http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch03.html#configuration-optional-dialects)

提交回复
热议问题