Hibernate - 基于外键的查询

目录

假设有两个表:

CREATE TABLE company (
  com_id int(11) NOT NULL auto_increment,
  com_name varchar(50) NOT NULL,
  PRIMARY KEY  (com_id),
  UNIQUE KEY com_id (com_id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE jobs (
  jobs_id int(11) NOT NULL auto_increment,
  com_id int(11) NOT NULL,
  jobs_name varchar(50) NOT NULL,
  jobs_email varchar(50) NOT NULL,
  PRIMARY KEY  (jobs_id),
  UNIQUE KEY jobs_id (jobs_id),
  KEY com_id (com_id),
  CONSTRAINT jobs_fk FOREIGN KEY (com_id) REFERENCES company(com_id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

其中company表对应Company类,jobs表对应Jobs类。

因为通过Hibernate的多对一关联的配置,com_id不出现在Jobs.hbm.xml文件的配置信息里,取而代之的是Company的配置,所以关于如何按jobs表的外键(com_id)查询通过baidu、google查询,发现了两种方式:

1.在DAO层的查询方法:

public List<Jobs> jobs_select(int com_id) {
        //session和事务操作......
        String hql="from Jobs as j where j.company.com_id =?";
        Query query=session.createQuery(hql);
        query.setInteger(0,com_id);
        return query.list();
    }

2.在DAO层的查询方法:

public List<Jobs> jobs_select(int com_id) {
        //session和事务操作......
        String hql="from Jobs as j where j.company=?";
        Query query=session.createQuery(hql);
        Company com=new Company();
        com.setCom_id(com_id);
        query.setParameter(0,com);
        return query.list();
    }

通过上述两种方式都可以实现基于外键的查询,如果有人发现更好的方式,请通过回复的方式贴上,让大家共同学习。

下面是Compay、Jobs类和Company.hbm.xml、Jobs.hbm.xml的代码:

public class Company {
    private int com_id;
    private String com_name;
    private Set jobs;
    //getter and setter......

}
public class Jobs {
    private int jobs_id;
    private int com_id;
    private String jobs_name;
    private String jobs_email;
    private Company company;
    //getter and setter......
}
Company.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.pojo.Company" table="company" catalog="testHib" dynamic-insert="true" dynamic-update="true">
        <id name="com_id" type="java.lang.Integer">
            <column name="com_id" />
            <generator class="identity"/>
        </id>
        <property name="com_name" type="java.lang.String">
            <column name="com_name" length="50" not-null="true" />
        </property>
        <set name="jobs" table="jobs" lazy="false" inverse="true" cascade="delete" sort="unsorted">
            <key column="com_id"/>
            <one-to-many class="com.pojo.Jobs"/>
        </set>
    </class>
</hibernate-mapping>
Jobs.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.pojo.Jobs" table="jobs" catalog="testHib" dynamic-insert="true" dynamic-update="true">
        <id name="jobs_id" type="java.lang.Integer">
            <column name="jobs_id"/>
            <generator class="identity"/>
        </id>
        <property name="jobs_name" type="java.lang.String">
            <column name="jobs_name" length="50" not-null="true" >
        </property>
        <property name="jobs_email" type="java.lang.String">
            <column name="jobs_email" length="50" not-null="true"/>
        </property>
        <many-to-one name="company" class="com.pojo.Company" cascade="none" outer-join="auto" column="com_id"/>
    </class>
</hibernate-mapping>

有问题请各位指教。

文章最后编辑时间为:2016-11-11 16:25:46

标签: java hibernate

声明:本博客如无特殊说明皆为原创,转载请注明来源:Hibernate - 基于外键的查询谢谢!

相关文章

发表评论:

icon_question.pngicon_razz.pngicon_sad.pngicon_evil.pngicon_exclaim.pngicon_smile.pngicon_redface.pngicon_biggrin.pngicon_surprised.pngicon_eek.pngicon_confused.pngicon_cool.pngicon_lol.pngicon_mad.pngicon_twisted.pngicon_rolleyes.pngicon_wink.pngicon_idea.pngicon_arrow.pngicon_neutral.pngicon_cry.pngicon_mrgreen.png