0

我需要在两个表之间进行内连接,但没有成功,我必须找到与特定用户相关的所有患者,我尝试查询但没有成功。我正在使用 mysql 和休眠 3.6.4。这是我的代码

病人.java

@Entity
public class Patient {

    @Id
    private int id;
    private String paitentFirstName;
    private String paitentLastName;
    private Date dateOfbirth;
    private String sex;
      @ManyToMany(cascade = {CascadeType.ALL})
        @JoinTable(name="User_Patient", 
                    joinColumns={@JoinColumn(name="id")}, 
                    inverseJoinColumns={@JoinColumn(name="userName")})
        private Set<User> users = new HashSet<User>();


    public Set<User> getUsers() {
        return users;
    }
    public void setMeetings(Set<User> users) {
        this.users = users;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getPaitentFirstName() {
        return paitentFirstName;
    }
    public void setPaitentFirstName(String paitentFirstName) {
        this.paitentFirstName = paitentFirstName;
    }
    public String getPaitentLastName() {
        return paitentLastName;
    }
    public void setPaitentLastName(String paitentLastName) {
        this.paitentLastName = paitentLastName;
    }
    public Date getDateOfbirth() {
        return dateOfbirth;
    }
    public void setDateOfbirth(Date dateOfbirth) {
        this.dateOfbirth = dateOfbirth;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Patient [id=" + id + ", paitentFirstName=" + paitentFirstName
                + ", paitentLastName=" + paitentLastName + ", dateOfbirth="
                + dateOfbirth + ", sex=" + sex + "]";
    }

}

用户.java

@Entity
public class User {



    @Id
    private String UserName;

     @ManyToMany(mappedBy="users")
        private Set<Patient> patients = new HashSet<Patient>();

    public String getUserName() {
        return UserName;
    }
    public void setUserName(String userName) {
        UserName = userName;
    }
    public Set<Patient> getEmployees() {
        return patients;
    }
    public void setEmployees(Set<Patient> patients) {
        this.patients = patients;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    private String password;
}

休眠配置文件

   <?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test2</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <!-- Names the annotated entity class -->
        <mapping class="com.objects.Patient"/>
        <mapping class="com.objects.User"/>

    </session-factory>

</hibernate-configuration>
4

1 回答 1

0

不需要显式连接。使用修复了HHH-5209 的Hibernate 版本,查询可以编写如下:

SELECT p
FROM Patient p
WHERE :particularUser MEMBER OF p.users

可以使用旧版本的 IN ELEMENTS 代替:

SELECT p
FROM Patient p
WHERE :particularUser IN ELEMENTS (p.users)
于 2013-07-15T19:11:03.403 回答