KJEjava48
KJEjava48

Reputation: 2055

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

In my application I came to a situation where i need to save some data to an entity but the entity name is unknown, and is saved in a variable and entity field and its value are in a list.So i need to pick the entity name from variable and its field and data from list. Variable is xEntity.

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

Some of the entity class are shown below which may come as the xEntity,and attributeList in function saveData().

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

Below are some of the values that may come to attributeList each time.

    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 value come as string date which should be formatted to type Date.Below is the Attribute 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;
        }

    }

Now can i use hibernate query to save data to each entity?If anyone know please help me.

Upvotes: 0

Views: 759

Answers (2)

Abhisar
Abhisar

Reputation: 186

Though I don't know why you want to do this but if you have no other choice then you have to use reflection here.

First create instance using your class name. It should be full class name. Also your classes should have default constructor.

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

Now use reflection to set the values of fields. Lets assume your field name is stored in fieldName and value is stored in fieldValue

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

For the cases where you need to convert the type from String to Date etc. You have to get field type and convert accordingly. Field type can be found out by:

declaredField.getFieldType();

Now save this object using hibernate.

sessionFactory.getCurrentSession().save(object);

Upvotes: 1

Tom
Tom

Reputation: 1027

I'm afraid HQL doesn't support inserting per query but you can use reflection to build your entities or create a native query.

Upvotes: 1

Related Questions