Hibernate: How specify custom sequence generator class name using annotations?

后端 未结 2 1883
陌清茗
陌清茗 2020-11-29 05:33

I want to specify following hbm configuration using annotations:


            &         


        
相关标签:
2条回答
  • 2020-11-29 05:57

    please find below set of code which i have used in project for the same.

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "XyzIdGenerator")
    @GenericGenerator(name = "XyzIdGenerator",
            strategy = "com.mycompany.myapp.id.BigIntegerSequenceGenerator",
            parameters = {
                @Parameter(name = "sequence", value = "xyz_id_sequence")
            })
    public BigInteger getId()
    {
       return id;
    }
    
    
    package com.mycompany.myapp.id;
    
    import org.hibernate.id.SequenceGenerator;
    ...
    
    public class BigIntegerSequenceGenerator
        extends SequenceGenerator
    {
        @Override
        public Serializable generate(SessionImplementor session, Object obj)
        {
            ...
        }
    }
    
    0 讨论(0)
  • 2020-11-29 06:07

    please refer the custom-id-generator-in-hibernate this may help you. In this example, I am creating the sequence number by taking MAX number from primary key table called pk_table.

    Employee Class looks as follows.

    @Entity
    @Table(name="EMPLOYEE")
    public class Employee {
    
    @Id
    @GenericGenerator(name = "sequence_emp_id", strategy = "com.supportmycode.model.EmployeeIdGenerator")
    @GeneratedValue(generator = "sequence_emp_id")  
    @Column(name="employee_id")
    private String employeeId;
    
    @Column(name="firstname")
    private String firstname;
    
    @Column(name="lastname")
    private String lastname;
    
    @Column(name="birth_date")
    private Date birthDate;
    
    @Column(name="cell_phone")
    private String cellphone;
    
    //Getter and Setter methods.
    }
    

    EmployeeIdGenerator class looks as follows

    public class EmployeeIdGenerator implements IdentifierGenerator {
    
    public Serializable generate(SessionImplementor session, Object object)
            throws HibernateException {
    
        String prefix = "EMP";
        Connection connection = session.connection();
        try {
    
            PreparedStatement ps = connection
                    .prepareStatement("SELECT MAX(value) as value from hibernate_tutorial.pk_table");
    
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("value");
                String code = prefix + new Integer(id).toString();
                System.out.println("Generated Stock Code: " + code);
                return code;
            }
    
        } catch (SQLException e) {       
            e.printStackTrace();
        }
        return null;
    }
    
    }
    

    Here, we have Implemented the interface IdentifierGenerator to override the function generate(SessionImplementor session, Object object). the statement SELECT MAX(value) as value from hibernate_tutorial.pk_table will get the MAX number from the table pk_table. And then we prefix the MAX number with the string "EMP".

    Output looks as follows:

    EMPLOYEE

    ------------------------------------------------------------
    employee_id | birth_date | cell_phone | firstname | lastname
    ------------------------------------------------------------
    EMP1        | 2014-08-22 | 111        | Nina      |  Mayers
    
    0 讨论(0)
提交回复
热议问题