繁体   English   中英

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

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

我一直在研究一个现在处于生产模式的项目。 现在我被告知要从.hbm.xml文件中完全删除映射,这样我就需要在程序代码中手动处理每个关系。 这是一个很大的问题因为我编写的每个数据库操作都是在Hibernate 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();

这是在.hbm.xml文件中存在所有关系时编写的条件。 现在,您可以了解从.hbm.xml文件中删除映射时将要遇到的问题。 TBH,我要通过删除Criteria并用HQL替换它来修改整个DAO类。 此外,我将无法使用HQL直接将结果作为对象获取。

是否可以只对标准进行小的更改(比如在条件本身中定义表之间的连接),这样即使从.hbm.xml文件中删除映射后,我也会获得相同的输出。

是的,您可以在Entity类中使用Java Persistence Annotations ,其工作方式与.hbm.xml类相同。

以此为例

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

    }


然后你就像过去那样使用标准。

暂无
暂无

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

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