簡體   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