如何映射数据库hql

时间:2016-05-01 07:29:53

标签: java hibernate hql one-to-many many-to-one

我有2个简单表格的数据库。

带有id,名称的猫(类别)。

产品(id,catid,名称,价格)。

我提出了一个问题

select pr.id, pr.catid,pr.name, pr.price 
FROM prod pr, cat ct 
WHERE pr.catid = ct.id AND ct.name = ?

当我在prod类中使用@JoinColumn(name="catid")时,我注意到了问题搜索引擎。

  

实体映射中的重复列:goods.prod列:catid   (应使用insert =“false”update =“false”

进行映射

我尝试使用@JoinColumn(name="catid",insertable = false, updatable = false) 但即便如此,我也犯了错误。

Ljava.lang.Object; cannot be cast to goods.prod

如何映射数据库?我是否需要在最简单的SQL中进行更改?

猫类

package goods;

import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;
import goods.prod;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="cat")   
public class cat implements Serializable {

     @Id
     @Column(name="id",insertable = false, updatable = false)
     private Integer id;

     @OneToMany(mappedBy="catmap")
     private List<prod> prods;

    private static final long serialVersionUID = -4147058093508047162L;

    private String name;

    public cat() {
    }

    public cat(int id, String Name) {
        this.id = id;
        this.name = Name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String Name) {
        this.name = Name;
    }

    }

prod class

package goods;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import goods.cat;

import java.io.Serializable;


@Entity
@Table(name = "prod")    
public class prod implements Serializable {

    @Id @GeneratedValue 
    @Column(name="id",insertable = false, updatable = false)
    private Long id;

    @ManyToOne (fetch=FetchType.LAZY)
    @JoinColumn(name="catid",insertable = false, updatable = false)
    private cat catmap;

    private Integer catid;

    private String name;

    private Integer price;

    public prod() {
    }

    public prod(Long id, Integer catid, String name, Integer price) {
        this.id = id;
        this.catid = catid;
        this.name = name ;
        this.price = price;
    }

    public Long getid() {
        return id;
    }

    public void setid(Long id) {
        this.id = id;
    }

    public Integer getcatid() {
        return catid;
    }

    public void setcatid(Integer catid) {
        this.catid = catid;
    }

    public String getname() {
        return name;
    }    

    public void setname(String name) {
        this.name = name;
    }

    public Integer getprice() {
        return price;
    }   

    public void setprice(Integer price) {
        this.price = price;
    }

    }

FUNC

@SuppressWarnings("unchecked")
public  static ArrayList<prod> getListOfProds(String catname,String name,Integer pricel, Integer priceh){
    ArrayList<prod> list = new ArrayList<prod>();
    Session session = HibernateUtil.openSession();
    Transaction tx = null;        
    try {
        tx = session.getTransaction();
        tx.begin();
        //Query query = session.createQuery("FROM prod");
        Query query = session.createQuery("select pr.id, pr.catid,pr.name, pr.price FROM prod pr, cat ct WHERE pr.catid = ct.id AND ct.name = ?");
        //Query query = session.createQuery("from prod pr join pr.cat ct with pr.catid=ct.id where ct.name=?");
        query.setString(0, catname);
        //query.setInteger(1, pricel);
        //query.setInteger(2, priceh);
        list = (ArrayList<prod>) query.list(); 

        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
    return list;
}      

0 个答案:

没有答案