JPA +使用连接表或第三表的一对多和多对一

时间:2014-01-28 13:05:18

标签: java hibernate jpa orm

我正在尝试在one-to-manymany-to-one实体之间建立QueryResponse关系。

public class Query extends IdentifiableEntity {
    private String complaint;
    private List<Response> response;
    //Setter getter
}

public class Response extends IdentifiableEntity {
    private String response;
    private Query query;
    //Setter getter
  }

public abstract class IdentifiableEntity implements Serializable{

    private long id;
    //setter getter
}   

SQL DDL

CREATE TABLE IF NOT EXISTS `mydb`.`tbl_query` (
  `query_id` INT NOT NULL AUTO_INCREMENT,
  `complaint` VARCHAR(2000) NOT NULL,
  `query_fut1` VARCHAR(50) NULL,
  `query_fut2` VARCHAR(45) NULL,
  PRIMARY KEY (`query_id`))
ENGINE = InnoDB;


CREATE TABLE IF NOT EXISTS `mydb`.`tbl_response` (
  `response_id` INT NOT NULL AUTO_INCREMENT,
  `response_desc` BLOB NOT NULL,
  `response_fut1` VARCHAR(45) NULL,
  `response_fut2` VARCHAR(45) NULL,
  PRIMARY KEY (`response_id`))
ENGINE = InnoDB; 


CREATE TABLE IF NOT EXISTS `mydb`.`tbl_map_query_response` (
  `map_query_response_id` INT NOT NULL AUTO_INCREMENT,
  `query_id` INT NOT NULL,
  `response_id` INT NOT NULL,
  `map_query_response_fut1` VARCHAR(45) NULL,
  `map_query_response_fut2` VARCHAR(45) NULL,
  PRIMARY KEY (`map_query_response_id`),
  INDEX `fk_tbl_map_query_response_tbl_query1_idx` (`query_id` ASC),
  INDEX `fk_tbl_map_query_response_tbl_response1_idx` (`response_id` ASC),
  CONSTRAINT `fk_tbl_map_query_response_tbl_query1`
    FOREIGN KEY (`query_id`)
    REFERENCES `mydb`.`tbl_query` (`query_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_tbl_map_query_response_tbl_response1`
    FOREIGN KEY (`response_id`)
    REFERENCES `mydb`.`tbl_response` (`response_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

映射配置

<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="Query" class="com.qa.models.Query" cacheable="false">
        <table name="tbl_query" />
        <attribute-override name="id">
            <column name="query_id" nullable="false" column-definition="INT" />
        </attribute-override>
        <attributes>
            <basic name="complaint">
                <column name="complaint" nullable="false" />
            </basic>

            <one-to-many name="response">
                <join-table name="tbl_map_query_response">
                    <join-column name="query_id" referenced-column-name="query_id" />
                    <inverse-join-column name="response_id" referenced-column-name="response_id" unique="true" />
                </join-table>
                <cascade>
                    <cascade-all />
                </cascade>
            </one-to-many>
        </attributes>
    </entity>


    <entity name="Response" class="com.qa.models.Response" cacheable="false">
        <table name="tbl_response" />
        <attribute-override name="id">
            <column name="response_id" nullable="false" column-definition="INT" />
        </attribute-override>
        <attributes>
            <basic name="response">
                <column name="response_desc" />
            </basic>
            <many-to-one name="query">
                <join-table name="tbl_map_query_response">
                    <join-column name="response_id" referenced-column-name="response_id" />
                    <inverse-join-column name="query_id" referenced-column-name="query_id" unique="true" />
                </join-table>
              <cascade>
                  <cascade-all />
              </cascade>
            </many-to-one>
        </attributes>
    </entity>  

我的问题是,这是在实体之间建立关系的正确方法吗?我遵循这种方法使应用程序模块化,假设将来Response tbl映射到任何其他表。那么,如果不改变Response的结构我怎么能达到要求?

0 个答案:

没有答案