Sleep: Call SequenceGenerator manually?

I wrote my own IdGenerator:

public class AkteIdGenerator implements IdentifierGenerator { public Serializable generate(SessionImplementor session, Object object) throws HibernateException { // if custom id is set -> use this id if (object instanceof SomeBean) { SomeBean someBean = (SomeBean) object; Long customId = someBean.getCustomId(); if (customId != 0) { return customId; } } // otherwise --> call the SequenceGenerator manually SequenceStyleGenerator sequenceGenerator ... } } 

Does anyone know how I can name a sequence generator from my generator class, which I can usually define for annotations:

 @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "MY_SEQUENCE") @SequenceGenerator( allocationSize = 1, name = "MY_SEQUENCE", sequenceName = "MY_SEQUENCE_NAME") 

I would be very grateful for any decisions !!!!

Thanks a lot, Norbert

+7
source share
4 answers

You can call SequenceGenerator from the Generator class with a call. By writing this code. The custom generator class must be

  public class StudentNoGenerator implements IdentifierGenerator { public Serializable generate(SessionImplementor session, Object object)throws HibernateException { SequenceGenerator generator=new SequenceGenerator(); Properties properties=new Properties(); properties.put("sequence","Stud_NoSequence"); generator.configure(Hibernate.STRING, properties, session.getFactory().getDialect()); return generator.generate(session, session); } 

}
In the above code, Stud_NoSequence is the name of the sequence that shoulb will create. in the database by undoing create sequence Stud_NoSequence; Hibernate.String is the type returned by the SequenceGenerator class.

and the domain class will be

 import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity @org.hibernate.annotations.GenericGenerator( name = "Custom-generator", strategy = "com.ssis.id.StudentNoGenerator" ) public class Student { @Id @GeneratedValue(generator = "Custom-generator") String rno; @Column String name; public String getRno() { return rno; } public void setRno(String rno) { this.rno = rno; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 
+6
source
  @Id @GenericGenerator(name = "seq_id", strategy = "de.generator.AkteIdGenerator") @GeneratedValue(generator = "seq_id") @Column(name = "ID") private Integer Id; 

http://blog.anorakgirl.co.uk/2009/01/custom-hibernate-sequence-generator-for-id-field/

+1
source

Your message was useful for updating the sequence name.

Because I use monthly sequence and configuration does not update every id generation.

Here is my code:

 @Override public Serializable generate(SessionImplementor sessionImplementator, Object object) throws HibernateException { Calendar now = Calendar.getInstance(); // If month sequence is wrong, then reconfigure. if (now.get(Calendar.MONTH) != SEQUENCE_DATE.get(Calendar.MONTH)) { super.configure(new LongType(), new Properties(), sessionImplementator.getFactory().getDialect()); } Long id = (Long) super.generate(sessionImplementator, object); String sId = String.format("%1$ty%1$tm%2$06d", SEQUENCE_DATE, id); return Long.parseLong(sId);// 1301000001 } 
0
source

I'm not sure if this will help, but I continued to run into this message while searching for my answer, which I did not find anywhere, but found the solution myself. So I thought this might be the best place to share.

If you use hibernate as a JPA provider, you can manually call the identifier generator assigned to this entity class. First enter the JpaContext:

 @Autowired org.springframework.data.jpa.repository.JpaContext jpaContext; 

Then get the internal org.hibernate.id.IdentifierGenerator with this:

 org.hibernate.engine.spi.SessionImplementor session = jpaContext.getEntityManagerByManagedType(MyEntity.class).unwrap(org.hibernate.engine.spi.SessionImplementor.class); org.hibernate.id.IdentifierGenerator generator = session.getEntityPersister(null, new MyEntity()).getIdentifierGenerator(); 

Now you can get the identifier from the generator programmatically:

 Serializable id = generator.generate(session, new MyEntity()); 
0
source

All Articles