문서의 이전 판입니다!
hibernate.id.new_generator_mappings=true
프라퍼티를 통해 새로운 방식으로 ID를 생성할 수 있다. 특히 Sequence에 해당하는 방식인듯.@Entity public class EntityWithIdentityId { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) Long id; : }
@Entity // Define a sequence - might also be in another class: @SequenceGenerator(name="seq", initialValue=1, allocationSize=100) public class EntityWithSequenceId { // Use the sequence that is defined above: @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq") @Id Long id; }
@Entity @TableGenerator(name="tab", initialValue=0, allocationSize=50) public class EntityWithTableId { @GeneratedValue(strategy=GenerationType.TABLE, generator="tab") @Id Long id; }
public class StockCodeGenerator implements IdentifierGenerator { private static Logger log = Logger.getLogger(StockCodeGenerator.class); public Serializable generate(SessionImplementor session, Object object) throws HibernateException { String prefix = "M"; Connection connection = session.connection(); try { PreparedStatement ps = connection .prepareStatement("SELECT nextval ('seq_stock_code') as nextval"); ResultSet rs = ps.executeQuery(); if (rs.next()) { int id = rs.getInt("nextval"); String code = prefix + StringUtils.leftPad("" + id,3, '0'); log.debug("Generated Stock Code: " + code); return code; } } catch (SQLException e) { log.error(e); throw new HibernateException( "Unable to generate Stock Code Sequence"); } return null; } }
@Id @GenericGenerator(name="seq_id", strategy="my.package.StockCodeGenerator") @GeneratedValue(generator="seq_id") @Column(name = "stock_code", unique = true, nullable = false, length = 20) public String getStockCode() { return this.stockCode; }