We tried upgrading hibernate version in out project form 5.2.X to 5.2.13.Final , but new hibernate correctly forces
The scope of the generator name is global to the persistence unit (across all generator types).
Our clas structure consists of one BaseEntity and subclasses with defined @GenericGenerator, all generators use the same name.
@MappedSuperclass
public class BaseEntity {
@Id
@Column(name="ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="idGenerator")
private Long id;
}
@Entity
@Table(name = "SAMPLE_TABLE")
@GenericGenerator(strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", name = "idGenerator", parameters = {
@Parameter(name = SequenceStyleGenerator.SEQUENCE_PARAM, value = "SAMPLE_SEQ") })
public class SampleEntity extends BaseEntity
Is there an option to correct this generators definitions without placing id field in every class?
This worked for us on Hibernate 5.2.1 & postgres:
import org.hibernate.annotations.GenericGenerator
import org.hibernate.annotations.Parameter
import org.hibernate.id.enhanced.SequenceStyleGenerator
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.MappedSuperclass
@MappedSuperclass
abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@GenericGenerator(name = "sequenceGenerator", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = [
Parameter(name = SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY, value = "true"),
Parameter(name = SequenceStyleGenerator.OPT_PARAM, value = "pooled"),
Parameter(name = SequenceStyleGenerator.INITIAL_PARAM, value = "1"),
Parameter(name = SequenceStyleGenerator.INCREMENT_PARAM, value = "50")
]
)
var id: Long? = null
}
Code is in kotlin but you could easily transform it into java.
If you need more info on SequenceStyleGenerator, see docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With