如果实体名称保存在变量中,如何编写休眠插入查询

How to write hibernate insert query if entity name is saved in a variable

在我的应用程序中,我遇到了这样一种情况,我需要将一些数据保存到一个实体,但实体名称是未知的,并且保存在一个变量和实体字段中,它的值在 list.So我需要从变量中选择实体名称及其字段和列表中的数据。 变量是 xEntity。

public void saveData(String xEntity,List<Attribute> attributeList) {
  // hibernate insert query
}

一些实体 class 如下所示,可能作为函数 saveData() 中的 xEntity 和 attributeList。

@Entity
    @Table(name = "person")
    public class Person implements Serializable {
        @Id
        @GeneratedValue
        @Column(name = "id")
        private Integer id;

        @Column(name = "emp_id")
        private String empId;

        @Column(name = "name")
        private String name;

        @Column(name="dob")
        private Date dob;

        @Column(name="active")
        private Boolean active;

        @Column(name="created_on")
        private Timestamp createdOn;

        public Person() {
        }

        public Person(Integer id, String empId, String name, Date dob, Boolean active, Timestamp createdOn) {
            this.id = id;
            this.empId = empId;
            this.name = name;
            this.dob = dob;
            this.active = active;
            this.createdOn = createdOn;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getEmpId() {
            return empId;
        }

        public void setEmpId(String empId) {
            this.empId = empId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Date getDob() {
            return dob;
        }

        public void setDob(Date dob) {
            this.dob = dob;
        }

        public Boolean getActive() {
            return active;
        }

        public void setActive(Boolean active) {
            this.active = active;
        }

        public Timestamp getCreatedOn() {
            return createdOn;
        }

        public void setCreatedOn(Timestamp createdOn) {
            this.createdOn = createdOn;
        }
    }


    @Entity
    @Table(name = "person_address")
    public class PersonAddress implements Serializable {
        @Id
        @GeneratedValue
        @Column(name = "id")
        private Integer id;

        @Column(name = "emp_id")
        private String empId;

        @Column(name = "house_name")
        private String houseName;

        @Column(name="post_office")
        private String postOffice;

        @Column(name="district")
        private String district;

        @Column(name="state")
        private String state;

        @Column(name="active")
        private Boolean active;

        @Column(name="created_on")
        private Timestamp createdOn;

        public PersonAddress() {
        }

        public PersonAddress(Integer id, String empId, String houseName, String postOffice, String district, String state, Boolean active, Timestamp createdOn) {
            this.id = id;
            this.empId = empId;
            this.houseName = houseName;
            this.postOffice = postOffice;
            this.district = district;
            this.state = state;
            this.active = active;
            this.createdOn = createdOn;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getEmpId() {
            return empId;
        }

        public void setEmpId(String empId) {
            this.empId = empId;
        }

        public String getHouseName() {
            return houseName;
        }

        public void setHouseName(String houseName) {
            this.houseName = houseName;
        }

        public String getPostOffice() {
            return postOffice;
        }

        public void setPostOffice(String postOffice) {
            this.postOffice = postOffice;
        }

        public String getDistrict() {
            return district;
        }

        public void setDistrict(String district) {
            this.district = district;
        }

        public void setState(String state) {
            this.state = state;
        }

        public Boolean getActive() {
            return active;
        }

        public void setActive(Boolean active) {
            this.active = active;
        }

        public Timestamp getCreatedOn() {
            return createdOn;
        }

        public void setCreatedOn(Timestamp createdOn) {
            this.createdOn = createdOn;
        }
    }


    @Entity
    @Table(name = "person_qualification")
    public class PersonQualification implements Serializable {
        @Id
        @GeneratedValue
        @Column(name = "id")
        private Integer id;

        @Column(name = "emp_id")
        private String empId;

        @Column(name = "degree")
        private String degree;

        @Column(name="grade")
        private String grade;

        @Column(name="active")
        private Boolean active;

        @Column(name="created_on")
        private Timestamp createdOn;

        public PersonQualification() {
        }

        public PersonQualification(Integer id, String empId, String degree, String grade, Boolean active, Timestamp createdOn) {
            this.id = id;
            this.empId = empId;
            this.degree = degree;
            this.grade = grade;
            this.active = active;
            this.createdOn = createdOn;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getEmpId() {
            return empId;
        }

        public void setEmpId(String empId) {
            this.empId = empId;
        }

        public String getDegree() {
            return degree;
        }

        public void setDegree(String degree) {
            this.degree = degree;
        }

        public String getGrade() {
            return grade;
        }

        public void setGrade(String grade) {
            this.grade = grade;
        }

        public Boolean getActive() {
            return active;
        }

        public void setActive(Boolean active) {
            this.active = active;
        }

        public Timestamp getCreatedOn() {
            return createdOn;
        }

        public void setCreatedOn(Timestamp createdOn) {
            this.createdOn = createdOn;
        }
    }       

以下是attributeList每次可能会出现的一些值。

    attributeList = [{"fieldName":"empId","value":"EMP_123"},{"fieldName":"name","value":"Basil"},{"fieldName":"dob","value":"26-11-90"}]

    attributeList = [{"fieldName":"empId","value":"EMP_123"},{"fieldName":"houseName","value":"Ellikkal"},{"fieldName":"postOffice","value":"Chengara"},{"fieldName":"district","value":"Alappy"},{"fieldName":"state","value":"Kerala"}]

    attributeList = [{"fieldName":"empId","value":"EMP_123"},{"fieldName":"degree","value":"B.Tech"},{"fieldName":"grade","value":"First class"}]

日期值作为字符串日期应格式化为类型 Date.Below 是属性 class。

        public class Attribute implements Serializable {
        private String fieldName;
        private String value;

        public Attribute() {
            super();
            // TODO Auto-generated constructor stub
        }

        public Attribute(String fieldName, String value) {
            this.fieldName = fieldName;
            this.value = value;
        }

        public String getFieldName() {
            return fieldName;
        }

        public void setFieldName(String fieldName) {
            this.fieldName = fieldName;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

    }

现在我可以使用 hibernate 查询将数据保存到每个实体吗?如果有人知道请帮助我。

恐怕 HQL 不支持按查询插入,但您可以使用反射来构建实体或创建本机查询。

虽然我不知道你为什么要这样做,但如果你别无选择,那么你必须在这里使用反射。

首先使用您的 class 名称创建实例。它应该是完整的 class 名称。此外,您的 classes 应该具有默认构造函数。

Class<?> clazz = Class.forName(className);
Constructor<?> ctor = clazz.getConstructor();
Object object = ctor.newInstance();

现在使用反射来设置字段的值。假设您的字段名称存储在 fieldName 中,值存储在 fieldValue

declaredField = object.getClass().getDeclaredField(fieldName);
declaredField.setAccessible(true);
declaredField.set(object, fieldValue);

对于需要将类型从字符串转换为日期等的情况,您必须获取字段类型并进行相应的转换。可以通过以下方式找到字段类型:

declaredField.getFieldType();

现在使用休眠保存此对象。

sessionFactory.getCurrentSession().save(object);