简体   繁体   中英

How to generate id for diferent child class entities using jpa and hibernate

I have following AbstractCompany super class entity:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class AbstractCompany {
@Id
    @GenericGenerator(name = "rju_id", strategy = "com.ivc.talonsystem.util.keygens.RjuIdGenerator")
    @GeneratedValue(generator = "rju_id")  
    @Column(name = "id")
    private Integer id;
// other properties and getters-setters for them
}

and child class Rju:

@Entity
@Table(name="rju")
public class Rju extends AbstractCompany implements Serializable {
    @NotEmpty
    @Column(name = "fullname")
    private String namerju;
    @NotEmpty
    @Column(name = "briefname")
    private String briefname;
    // other properties and getters-setters for them
}

and RjuIdGenerator class for generating id for Rju entity:

public class RjuIdGenerator implements IdentifierGenerator {

    @Override
    public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
        Connection connection = session.connection();
        try {
            Statement statement=connection.createStatement();

            ResultSet rs=statement.executeQuery("select max(id) as Id from Rju");

            if(rs.next())
            {
                int id=rs.getInt(1)+1;
                Integer generatedId = new Integer(id);
                return generatedId;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

}

this RjuIdGenerator class generates id for all super and child classes. How I can add another generator classes for specific child entities? Thanks in advance!!!

Guys I've found a workaround for the question. All I did is check for instance of the object in the RjuIdGenerator's generate method then get back generated id for specific class using appropriate table querying:

public class RjuIdGenerator implements IdentifierGenerator {

    @Override
    public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
        Connection connection = session.connection();
        try {
            Statement statement=connection.createStatement();
            if (object.getClass().getName()==Rju.class.getName()) {
                ResultSet rs=statement.executeQuery("select max(id) as Id from Rju");

                if(rs.next())
                {
                    int id=rs.getInt(1)+1;
                    Integer generatedId = new Integer(id);
                    return generatedId;
                }
            } else {
                ResultSet rs=statement.executeQuery("select max(id) as Id from AbstractCompany");

                if(rs.next())
                {
                    int id=rs.getInt(1)+100000;
                    Integer generatedId = new Integer(id);
                    return generatedId;
                }
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM