Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a sequence named hibernate_sequence being created with JPA using Hibernate with the Oracle 10g dialect?

All my entities use this type of @Id

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MYENTITY_SEQ")
@SequenceGenerator(name = "MYENTITY_SEQ", sequenceName = "MYENTITY_SEQ")
@Column(name = "MYENTITY", nullable = false)
private Long id;

or

@Id
@Column(name = "MYENTITY")

I find that an Oracle sequence named hibernate_sequence is always created. Why is this so? And how can I avoid this?

I am using JPA1 with Hibernate 3 and the Oracle 10g dialect.

like image 526
JavaRocky Avatar asked Jun 17 '10 01:06

JavaRocky


People also ask

What is hibernate_sequence used for?

Notice that, by default, the hibernate_sequence is used for all entities using the SEQUENCE identifier generation strategy without an explicit database sequence name. Notice that the hibernate_sequence was called five times since, by default, no sequence call optimizer is used.

What is sequence generator spring boot JPA?

The SequenceGenerator annotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. A sequence generator may be specified on the entity class or on the primary key field or property.

How can you define a sequence generated primary key in Hibernate?

IDENTITY: Hibernate relies on an auto-incremented database column to generate the primary key, SEQUENCE: Hibernate requests the primary key value from a database sequence, TABLE: Hibernate uses a database table to simulate a sequence.

What is the use of @GeneratedValue strategy GenerationType identity?

GenerationType lets us define that strategy. Here @GeneratedValue(stratergy=GenerationType. IDENTITY) is telling our DB to store primary key in the identity column which is a default column in SQL for default auto incremented primary key generation.


2 Answers

The HIBERNATE_SEQUENCE is used with REVINFO-entity for creating revision numbers. If you want to use different sequence you should create your custom revision entity.

Help with that: http://docs.jboss.org/hibernate/envers/3.5/reference/en-US/html/revisionlog.html

like image 50
Matias Sundberg Avatar answered Oct 19 '22 18:10

Matias Sundberg


I see the following code in org.hibernate.id.SequenceGenerator:

public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
    ObjectNameNormalizer normalizer = ( ObjectNameNormalizer ) params.get( IDENTIFIER_NORMALIZER );
    sequenceName = normalizer.normalizeIdentifierQuoting(
            PropertiesHelper.getString( SEQUENCE, params, "hibernate_sequence" )
    );
    parameters = params.getProperty( PARAMETERS );

    if ( sequenceName.indexOf( '.' ) < 0 ) {
        final String schemaName = normalizer.normalizeIdentifierQuoting( params.getProperty( SCHEMA ) );
        final String catalogName = normalizer.normalizeIdentifierQuoting( params.getProperty( CATALOG ) );
        sequenceName = Table.qualify(
                dialect.quote( catalogName ),
                dialect.quote( schemaName ),
                dialect.quote( sequenceName )
        );
    }
    else {
        // if already qualified there is not much we can do in a portable manner so we pass it
        // through and assume the user has set up the name correctly.
    }

    this.identifierType = type;
    sql = dialect.getSequenceNextValString( sequenceName );
}

Where the third parameter of PropertiesHelper.getString(String, Properties, String) is the default property value.

So I'm tempted to say that, somewhere, you have an Id not "properly" annotated. Maybe you should perform a little debugging session.

like image 45
Pascal Thivent Avatar answered Oct 19 '22 16:10

Pascal Thivent