3

I've been working on a project which is now in production mode. Now I've been told to remove the mappings completely from the .hbm.xml files so that I need to handle every relationships manually in the program code. It's really a big problem coz the every DB operation which I've written is in Hibernate Criteria.

Just consider the following Criteria

Criteria criteria = getSession().createCriteria(Table1.class,"table1");
        criteria.createAlias("table1.table2", "table2")
        .createAlias("table1.table3", "table3")
        .createAlias("table3.table4", "table4")
        .createAlias("table3.table5", "table5")
        .setProjection(Projections.projectionList()
                .add(Projections.property("id"),"id")
                .add(Projections.property("c1"),"c1")
                .add(Projections.property("c2"),"c2")
                .add(Projections.property("c3"),"c3")
                .add(Projections.property("table2.c1"),"table2.c1")
                .add(Projections.property("table2.c2"),"table2.c2")
                .add(Projections.property("table3.c1"),"table3.c1")
                .add(Projections.property("table5.c1"),"table3.table5.c1"))
                .add(Restrictions.eq("table4.c1", Constants.STATUS_ENABLED))
                .setResultTransformer(new AliasToBeanNestedResultTransformer(Table1.class));
        return criteria.list();

This is the criteria which is written when all the relationships are present in .hbm.xml files. Now you can understand what will be problem I'm going to face when removing the mapping from .hbm.xml files. TBH, I've to rework entire DAO classes by removing Criteria and replacing it with HQL. Also I'll not be able to fetch the result directly as object by using HQL.

Is it possible to only make small changes to the criteria (like defining the join between tables in the criteria itself) so that I'll get the same output even after removing the mappings from .hbm.xml files..?

The Coder
  • 2,349
  • 5
  • 31
  • 55

1 Answers1

2

Yes you can use the Java Persistence Annotations in the Entity classes and will work the same way the .hbm.xml classes do.

Take this for example

    @Entity
    public class Employee {

        @SequenceGenerator(name="EMPLOYEE_SEQ", sequenceName="EMPLOYEE_SEQ", initialValue=1, allocationSize=1)
        @Id @GeneratedValue(strategy=GenerationType.AUTO, generator="EMPLOYEE_SEQ")
        private int id;
        @Column(nullable = false, length = 50) 
        private String name;

        @ManyToOne(targetEntity = Country.class, optional = true, fetch = FetchType.LAZY)
        @JoinColumn(name = "loanID", updatable = false, insertable = false)
        private Loan loan;
        @Column(name = "loanID", updatable = true, insertable = true)
        private Integer loanID;


        public int getId() {
            return id;
        }
        public void setCompanyID(int id) {
            this.id = id;
        }

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

        public Integer getLoanD() {
            return loanID;
        }
        public void getLoanD(Integer loanID) {
            this.loanID = loanID;
        }

    }


And then you just use the criterias like you used to.

pedromendessk
  • 3,190
  • 1
  • 23
  • 34
  • But I'm not permitted to use Annotations. Actually, I first used Annotations in model classes, but then I was ordered to remove annotations and configure it in .hbm.xml files. The field names and column names will be configured in .hbm.xml files, only change is I'll be removing mappings like and so on mappings. – The Coder Apr 22 '15 at 11:00
  • 1
    That seems a bit odd.. In that case the only way is using hql :/ That question has already been answered here http://stackoverflow.com/questions/720502/hibernate-criteria-joining-table-without-a-mapped-association and here http://stackoverflow.com/questions/25679783/hibernate-criteria-projection-without-mapped-association-of-tables – pedromendessk Apr 22 '15 at 11:06
  • Thanks. Looks like I've to convince my superior to don't remove the mappings. I'll be losing a lot of features if I'm going to replace Criteria with HQL. – The Coder Apr 22 '15 at 11:11