Can Hibernate criteria be used if you are modifying the hbm.xml files by removing all mapping relationships?

I am working on a project which is now in production mode. Now I was told to completely remove the mappings from the files .hbm.xml

so that I had to handle all the relationships manually in code. This is a really big problem because every DB job I have written is in the 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")
                .add(Restrictions.eq("table4.c1", Constants.STATUS_ENABLED))
                .setResultTransformer(new AliasToBeanNestedResultTransformer(Table1.class));
        return criteria.list();


These are the criteria that are recorded when all relationships are present in the .hbm.xml files. Now you can figure out what will be the problem I will run into when removing the mapping from the .hbm.xml files. TBH, I have to refactor entire DAO classes by removing criteria and replacing them with HQL. Also I won't be able to get the result directly as an object using HQL.

Is it possible only to make small changes to the criteria (for example, define the join between tables in the criteria themselves) to get the same result even after removing the mappings from the files .hbm.xml



source to share

1 answer

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

Take this for example

    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) {
   = id;

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

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



And then you just use the criteria as you are used to.



All Articles