简体   繁体   English

如果通过删除整个映射关系来更改hbm.xml文件,是否仍然可以使用Hibernate标准?

[英]Is it possible to still use Hibernate criteria, if changing the hbm.xml files by removing entire mapping relationships?

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. 现在我被告知要从.hbm.xml文件中完全删除映射,这样我就需要在程序代码中手动处理每个关系。 It's really a big problem coz the every DB operation which I've written is in Hibernate Criteria. 这是一个很大的问题因为我编写的每个数据库操作都是在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. 这是在.hbm.xml文件中存在所有关系时编写的条件。 Now you can understand what will be problem I'm going to face when removing the mapping from .hbm.xml files. 现在,您可以了解从.hbm.xml文件中删除映射时将要遇到的问题。 TBH, I've to rework entire DAO classes by removing Criteria and replacing it with HQL. TBH,我要通过删除Criteria并用HQL替换它来修改整个DAO类。 Also I'll not be able to fetch the result directly as object by using HQL. 此外,我将无法使用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..? 是否可以只对标准进行小的更改(比如在条件本身中定义表之间的连接),这样即使从.hbm.xml文件中删除映射后,我也会获得相同的输出。

Yes you can use the Java Persistence Annotations in the Entity classes and will work the same way the .hbm.xml classes do. 是的,您可以在Entity类中使用Java Persistence Annotations ,其工作方式与.hbm.xml类相同。

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. 然后你就像过去那样使用标准。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM