如何在hibernate中连接多个表

时间:2015-07-10 07:08:00

标签: java spring hibernate

我正在使用hibernate开发一个Web应用程序。

我在jsp servlet中创建的相同应用程序,但现在我使用的是springhibernate,但无法使用jsp/servlet

执行相同的操作

jsp代码

String sql = "select city from addres_table where id=2";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
String city = rs.getString("city");
String sql1 = "select name from user_table where city='"+city+"'";
Statement st1 = con.createStatement();
ResultSet rs1 = st1.executeQuery(sql1);
while(rs1.next()){
String username = rs.getString("name");
}
}

基本上我从city获取addres_table现在我正在name users city来自User_table

我想在休眠中做同样的事情,但我无法做到。

我从city获取了addres_table,但现在我如何使用该城市从name获取User_table

这是我的hibernate代码

@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Address> getProfessionById(long id) throws Exception {
session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Address.class);
cr.add(Restrictions.eq("uid", id));
List results = cr.list();
tx = session.getTransaction();
session.beginTransaction();
tx.commit();
return results;
}

请帮助我,我想使用这个城市从User_table中获取名称

地址类

@Entity
@Table(name="user_profession")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Address implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name="id")
private long id;

@Column(name="city")
private String city;

public long getId() {
    return id;
}


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

public String getCity() {
    return city;
}

    public void setCity(String city) {
    this.city = city;
}
}

用户类

@Entity
@Table(name="user_table")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class User implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name="id")
private long id;

@Column(name="full_name")
private String fullName;

public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}

public String getFullName() {
    return fullName;
}
public void setFullName(String fullName) {
    this.fullName = fullName;
}
}

1 个答案:

答案 0 :(得分:1)

首先,你必须映射两个实体之间的关系,改变你的类:

地址类:

@Entity
@Table(name="user_profession")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Address implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    @Column(name="id")
    private long id;

    @Column(name="city")
    private String city;

    //Map the users here
    @OneToMany(mappedBy="adress")
    private Set<User> users;
    //getter and setter

    public long getId() {
        return id;
    }


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

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

用户类:

@Entity
@Table(name="user_table")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    @Column(name="id")
    private long id;

    @Column(name="full_name")
    private String fullName;

    //The Adress mapping here
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "Adress_ID", referencedColumnName = "id")
    private Address address;

    public long getId() {
        return id;
    }
    public void setId(long id) {
    this.id = id;
    }

    public String getFullName() {
        return fullName;
    }
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
}

然后要获取具有特定城市的用户列表,请执行以下操作:

String hql = "SELECT user.name FROM USER_TABLE user WHERE user.address IN (SELECT address FROM ADDRESS_TABLE address WHERE address.id = :id)";
Query query = session.createQuery(hql);
query.setParameter("id",2);
List results = query.list();

修改

请改用此查询:

String hql = "from User u INNER JOIN u.address ad where ad.id = :id";
Query query = session.createQuery(hql);
query.setParameter("id",2);
List results = query.list();

你会得到你需要的东西。