Hibernate: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
Line 6: Line 6:


The Instant class represents a moment on the timeline in '''UTC''' with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).All three java.time.Local… classes are all lacking any concept of time zone or offset-from-UTC.
The Instant class represents a moment on the timeline in '''UTC''' with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).All three java.time.Local… classes are all lacking any concept of time zone or offset-from-UTC.
==Sequence Style==
<source lang="java">
@Id
@Column(name = "code", length = 8)
//@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "page_sqn")
//@SequenceGenerator(name = "page_sqn", sequenceName = "page_sqn", initialValue = 60466176, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "page_sqn")
@GenericGenerator(name  = "page_sqn", strategy = "org.chorke.academia.core.entity.Base36Style", parameters = {
    @Parameter(name = Base36Style.INITIAL_VALUE,  value = Base36Style.DIGIT_6_MIN),
    @Parameter(name = Base36Style.SEQUENCE_NAME,  value = "page_sqn"),
    @Parameter(name = Base36Style.ALLOCATION_SIZE, value = "1"),
})
private String code;
</source>
<source lang="java">
package org.chorke.academia.core.entity;
import java.io.Serializable;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;
public class Base36Style extends SequenceStyleGenerator {
    public static final String ALLOCATION_SIZE = INCREMENT_PARAM;
    public static final String SEQUENCE_NAME = SEQUENCE_PARAM;
    public static final String INITIAL_VALUE = INITIAL_PARAM;
    public static final int RADIX = 36;
    public static final String DIGIT_1_MIN = "0";
    public static final String DIGIT_2_MIN = "36";
    public static final String DIGIT_3_MIN = "1296";
    public static final String DIGIT_4_MIN = "46656";
    public static final String DIGIT_5_MIN = "1679616";
    public static final String DIGIT_6_MIN = "60466176";
    public static final String DIGIT_7_MIN = "2176782336";
    public static final String DIGIT_8_MIN = "78364164096";
    public static final String DIGIT_9_MIN = "2821109907456";
    public static final String DIGIT_X_MIN = "101559956668416";
    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        Serializable nextval = super.generate(session, object);
        String base36 = Long.toString((long) nextval, RADIX);
        return base36.toUpperCase();
    }
    @Override
    public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
        super.configure(type, params, serviceRegistry);
    }
}
</source>


==Challenges==
==Challenges==

Revision as of 07:57, 15 October 2020

Replacements

java.util.Date     => java.time.Instant
java.sql.Timestamp => java.time.Instant
java.sql.Date      => java.time.LocalDate
java.sql.Time      => java.time.LocalTime

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).All three java.time.Local… classes are all lacking any concept of time zone or offset-from-UTC.

Sequence Style

@Id
@Column(name = "code", length = 8)
//@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "page_sqn")
//@SequenceGenerator(name = "page_sqn", sequenceName = "page_sqn", initialValue = 60466176, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "page_sqn")
@GenericGenerator(name  = "page_sqn", strategy = "org.chorke.academia.core.entity.Base36Style", parameters = {
    @Parameter(name = Base36Style.INITIAL_VALUE,   value = Base36Style.DIGIT_6_MIN),
    @Parameter(name = Base36Style.SEQUENCE_NAME,   value = "page_sqn"),
    @Parameter(name = Base36Style.ALLOCATION_SIZE, value = "1"),
})
private String code;
package org.chorke.academia.core.entity;

import java.io.Serializable;
import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;

public class Base36Style extends SequenceStyleGenerator {
    public static final String ALLOCATION_SIZE = INCREMENT_PARAM;
    public static final String SEQUENCE_NAME = SEQUENCE_PARAM;
    public static final String INITIAL_VALUE = INITIAL_PARAM;
    public static final int RADIX = 36;

    public static final String DIGIT_1_MIN = "0";
    public static final String DIGIT_2_MIN = "36";
    public static final String DIGIT_3_MIN = "1296";
    public static final String DIGIT_4_MIN = "46656";
    public static final String DIGIT_5_MIN = "1679616";
    public static final String DIGIT_6_MIN = "60466176";
    public static final String DIGIT_7_MIN = "2176782336";
    public static final String DIGIT_8_MIN = "78364164096";
    public static final String DIGIT_9_MIN = "2821109907456";
    public static final String DIGIT_X_MIN = "101559956668416";

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        Serializable nextval = super.generate(session, object);
        String base36 = Long.toString((long) nextval, RADIX);
        return base36.toUpperCase();
    }

    @Override
    public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
        super.configure(type, params, serviceRegistry);
    }
}

Challenges

Database Auto Increment Sequence Usable
DB2/LUW
DB2/z
PostgreSQL
Derby
Firebird
H2
HyperSQL
INGRES
Informix
MariaDB
MySQL
Oracle
Sql Server
SQLite
Sybase
Sybase Anywhere

References