简体   繁体   English

如何使用JPA和Hibernate为不同的子类实体生成ID

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

I have following AbstractCompany super class entity: 我有以下AbstractCompany超类实体:

@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: 和孩子类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: 和RjuIdGenerator类,用于为Rju实体生成ID:

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. 此RjuIdGenerator类为所有超级类和子类生成ID。 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: 我所做的只是在RjuIdGenerator的generate方法中检查对象的实例,然后使用适当的表查询获取特定类的生成ID:

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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JPA和Hibernate分离实体的关键通用类? - How to separate key common class for entities using JPA and Hibernate? 休眠 JPA 生成 id - Hibernate JPA Generate id 如何使用Hibernate或JPA动态生成实体集合? - How can i generate dynamically collections of entities with hibernate or jpa? 如何使用 Lombok 在 Spring/JPA/Hibernate 中获取带有所有子实体和子实体的父实体 - How to get parent entity with all child entities and child entities of children in Spring/JPA/Hibernate with Lombok 如何通过JPA / Hibernate在孩子的ID中引用父母的ID? - How to reference a parent's id in a child's id with JPA/Hibernate? 休眠(Spring JPA)子实体删除 - Hibernate (Spring JPA) child entities removal 通过Hibernate + JPA在两个实体中保存ID - Save id in both entities via Hibernate + JPA 如何使用 Hibernate/Spring JPA 为 Snowflake 表生成唯一的自动增量 ID? - How to generate Unique, AutoIncremented Id using Hibernate/Spring JPA for Snowflake table? 如何在 Spring JPA/Hibernate 中使用 JoinTable 仅通过 ID 设置引用父实体的子实体 - How to set up a child entity referencing parent by only ID using a JoinTable in Spring JPA/Hibernate 如何在JPA / Hibernate中基于两个外键生成ID? - How to generate id based on two foreign keys in JPA / Hibernate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM