Hibernate标准一对多关系

时间:2011-08-27 18:23:49

标签: database hibernate database-design

我有两个实体:DealersMakes。经销商有多个品牌。我能够将记录作为这两个实体之间的一对多关系插入。

但是,我无法根据经销商ID检索记录。我尝试了很多不同的方法,它仍然抛出这个例外:Property not found make(it is table) of : class name(full classname)

如果有人能提供一些提示,我们将不胜感激。

经销商:

 import java.util.HashSet;
    import java.util.Set;    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.OneToMany;
    import javax.persistence.JoinTable;
    import javax.persistence.CascadeType;
    import javax.persistence.JoinColumn;
    @Entity
    @Table(name = "dealer")
    public class Dealer implements java.io.Serializable{
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        public Dealer(){

        }
        public Dealer(String DealerName,String dealerPhno,Set makes){
            this.dealerName=DealerName;
            this.dealerphno=dealerPhno;
            this.dealerMakes=makes;

        }
        public void setDealerMakes(Set<Make> dealerMakes) {
            this.dealerMakes = dealerMakes;
        }
        @Id
        @GeneratedValue
        @Column(name = "dealer_id")
        private long dealerId;
        public long getDealerId() {
            return dealerId;
        }
        public void setDealerId(long dealerId) {
            this.dealerId = dealerId;
        }
        public String getDealerName() {
            return dealerName;
        }
        public void setDealerName(String dealerName) {
            this.dealerName = dealerName;
        }
        public String getDealerphno() {
            return dealerphno;
        }
        public void setDealerphno(String dealerphno) {
            this.dealerphno = dealerphno;
        }
        @Column(name = "dealer_name" ,unique=true, nullable = false, length=20)
        private String dealerName;
        @Column(name = "dealer_phno" , nullable = false, length=20)
        private String dealerphno;
        @OneToMany(cascade = CascadeType.ALL)
        //@OneToMany(fetch=FetchType.EAGER)
        @JoinColumn(name="dealer_id", nullable=false)
        //@JoinTable(name = "dealer_make", joinColumns = { @JoinColumn(name = "dealer_id") }, inverseJoinColumns = { @JoinColumn(name = "make_id") })
        private Set<Make> dealerMakes = new HashSet<Make>(0);

        public Set<Make> getDealerMakes() {
            return dealerMakes;
        }

    }

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "make")
public class Make implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 2L;
    public Make(){

    }
    public Make(String makeName,String modelName,String price){

        this.makeName=makeName;
        this.modelName=modelName;
        this.price=price;
    }
    public long getMakeId() {
        return makeId;
    }
    public void setMakeId(long makeId) {
        this.makeId = makeId;
    }
    public String getMakeName() {
        return makeName;
    }
    public void setMakeName(String makeName) {
        this.makeName = makeName;
    }
    public String getModelName() {
        return modelName;
    }
    public void setModelName(String modelName) {
        this.modelName = modelName;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    @Id
    @GeneratedValue
    @Column(name = "make_id")
    private long makeId;
    @Column(name = "make_name" , nullable = false, length=8)
    private String makeName;
    @Column(name = "model_name" , nullable = false, length=8)
    private String modelName;
    @Column(name = "price" , nullable = false, length=8)
    private String price;

}

查询我尝试过:

 Criteria dealer=sessionFactory.getCurrentSession().createCriteria(Dealer.class);
 Criteria make=dealer.createCriteria(make);
 make.add(Restrictions.gt("dealerId",new Long(dealerId)));
 List<Dealer> results=dealer.list(); //throwing exception

我期待的输出:

Dealer_Id dealername phonenumber

1 halal 074563485

制作以下记录:

make_id makename makeprice dealer_id

1 ford 3000 $ 1

2 hyundai 2000 $ 1

我想要详细了解经销商详情的结果。

3 个答案:

答案 0 :(得分:5)

行。因此,您希望使用其品牌加载经销商,并且您只希望经销商的ID大于给定ID。

首先,您可以加载经销商。当你打电话给getDealerMakes()

时,Hibernate会自动加载他们的品牌列表
Criteria c = session.createCriteria(Dealer.class);
c.add(Restrictions.gt("dealerId", dealerId)); // let's say delerId = 6
List<Dealer> dealers = c.list(); // execute SQL query select d.* from dealer d where d.dealer_id > 6
for (Dealer dealer : dealers) {
    Set<Make> make = dealer.getDealerMakes() // execute SQL query select m.* from make m where m.dealer_id = <theIdOfTheCurrentDealer>
}

这具有执行N + 1个查询的缺点。

因此,如果您想在单个查询中加载经销商及其品牌,则需要设置获取模式以便加载品牌。请注意,由于连接使用您在Dealer和Make之间定义的关联,您不需要告诉Hibernate经销商表的ID必须等于make表的dealer_id:Hibernate已经知道,感谢JoinColumn您放在关联上的注释。因此查询如下:

Criteria c = session.createCriteria(Dealer.class);
c.add(Restrictions.gt("dealerId", dealerId));

// this tells Hibernate that the makes must be fetched from the database
// you must use the name of the annotated field in the Java class: dealerMakes
c.setFetchMode("dealerMakes", FetchMode.JOIN);

// Hibernate will return instances of Dealer, but it will return the same instance several times
// once per make the dealer has. To avoid this, you must use a distinct root entity transformer
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<Delaer> dealers = c.list(); // executes the SQL query:
// select d.*, m.* from dealer d left join make m on d.dealer_id = m.dealer_id where d.dealer_id > 6
for (Dealer dealer : dealers) {
    Set<Make> make = dealer.getDealerMakes() // no SQL query. The makes are already loaded
}

答案 1 :(得分:1)

    public List<Dealer> findDealers() {
    Criteria dealer=sessionFactory.getCurrentSession().createCriteria(Dealer.class);
    Criteria make=dealer.createCriteria(make);
    make.setFetchMode("dealer", FetchMode.JOIN);
    make.add(Restrictions.gt("dealerId",new Long(dealerId)));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); // depends on wat u want
    return criteria.list();
    }

尝试像这样返回,希望它有效,请告诉我是否还有更多内容

答案 2 :(得分:1)

criteria.setFetchMode("dealerMakes", FetchMode.JOIN);
criteria.createCriteria("dealerMakes");
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

这就是诀窍!