0
我想要獲取具有特定Tag
的所有Query
對象。JPA +如何選擇使用第三個表作爲一對多關係中的連接記錄?
實體類新:
public abstract class IdentifiableEntity implements Serializable{
private long id;
//set/get
}
public class Query extends IdentifiableEntity {
private String query;
private Set<Tag> tags;
//set/get
}
public class Tag extends IdentifiableEntity {
private String tagName;
//set/get
}
表:
CREATE TABLE IF NOT EXISTS `mydb`.`tbl_map_query_tag` (
`map_query_tag_id` INT NOT NULL AUTO_INCREMENT,
`query_id` INT NOT NULL,
`tag_id` INT NOT NULL,
`comments` VARCHAR(100) NULL,
`map_query_tag_fut1` VARCHAR(45) NULL,
`map_query_tag_fut2` VARCHAR(45) NULL,
PRIMARY KEY (`map_query_tag_id`),
INDEX `fk_tbl_map_query_tag_tbl_query_idx` (`query_id` ASC),
INDEX `fk_tbl_map_query_tag_tbl_tag1_idx` (`tag_id` ASC),
CONSTRAINT `fk_tbl_map_query_tag_tbl_query`
FOREIGN KEY (`query_id`)
REFERENCES `mydb`.`tbl_query` (`query_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_tbl_map_query_tag_tbl_tag1`
FOREIGN KEY (`tag_id`)
REFERENCES `mydb`.`tbl_tag` (`tag_id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `mydb`.`tbl_query` (
`query_id` INT NOT NULL AUTO_INCREMENT,
`query_desc` VARCHAR(2000) NOT NULL,
PRIMARY KEY (`query_id`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `mydb`.`tbl_tag` (
`tag_id` INT NOT NULL AUTO_INCREMENT,
`tag_name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`tag_id`))
ENGINE = InnoDB;
orm.xml中
<?xml version="1.0" encoding="utf-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<mapped-superclass class="com.qa.models.IdentifiableEntity">
<attributes>
<id name="id">
<column name="id" nullable="false" column-definition="INT" />
<generated-value strategy="AUTO" />
</id>
</attributes>
</mapped-superclass>
<entity name="Tag" class="com.qa.models.Tag" cacheable="true">
<table name="tbl_tag" />
<attribute-override name="id">
<column name="tag_id" nullable="false" column-definition="INT" />
</attribute-override>
<attributes>
<basic name="tagName">
<column name="tag_name" nullable="false" />
</basic>
</attributes>
</entity>
<entity name="Query" class="com.qa.models.Query" cacheable="true">
<table name="tbl_query" />
<attribute-override name="id">
<column name="query_id" nullable="false" column-definition="INT" />
</attribute-override>
<attributes>
<basic name="query">
<column name="query_desc" nullable="false" />
</basic>
<one-to-many name="tags" target-entity="com.qa.models.Tag" fetch="LAZY">
<join-table name="tbl_map_query_tag">
<join-column name="query_id" referenced-column-name="query_id" />
<inverse-join-column name="tag_id" referenced-column-name="tag_id" unique="true" />
</join-table>
<cascade>
<cascade-all />
</cascade>
</one-to-many>
</attributes>
</entity>
</entity-mappings>
等效SQL查詢
select q.* from tbl_query q left outer join tbl_map_query_tag tm on q.query_id=tm.query_id left outer join tbl_tag t on t.tag_id=tm.tag_id where t.tag_id=:tagId
吾道方法是
public List<Query> searchQueryByTag(Tag tag) throws QADAOException {
javax.persistence.Query query = getEntityManager().createQuery(SQL_SEL_QUERY_TAG_ID);
query.setParameter("tag", tag);
List<Query> queries = query.getResultList();
return queries;
}
當我轉換此查詢到JPQL
這是行不通的。我的問題是我如何使用JPQL
實現等效於SQL查詢結果的結果?