表tx_account
account_id int,
name varchar(50);
....
表TxAgentRecordTime
agent_id int,
................
2张表的映射文件如下:
<hibernate-mapping package="com.callray.domain">
<class name="TxAccount" table="tx_account">
<id name="id" column="account_id">
<generator class="identity" />
</id>
<property name="name" column="account_name"/>
<one-to-one name="recordTime" class="TxAgentRecordTime" cascade="all"/>
</class>
</hibernate-mapping>
<class name="TxAgentRecordTime" table="tx_AgentRecordTime">
<id name="agentId" column="agent_id">
<generator class="foreign" >
<param name="property">account</param>
</generator>
</id>
<property name="mondaySet" column="monday_set"/>
<property name="tuesdaySet" column="tuesday_set"/>
<one-to-one name="account" class="TxAccount" constrained="true"/>
</class>
如下方式调用
public List<TxAccount> getAll() {
List<TxAccount> accounts = getSession().createQuery("FROM TxAccount")
.list();
closeSession();
return accounts;
}
hibernate生成的SQL语句
Hibernate:
select
txaccount0_.account_id as account1_0_,
txaccount0_.account_name as account2_0_,
from
tx_account txaccount0_
Hibernate:
select
txagentrec0_.agent_id as agent1_1_0_,
txagentrec0_.monday_set as monday2_1_0_,
from
tx_AgentRecordTime txagentrec0_
where
txagentrec0_.agent_id=?
Hibernate:
select
txagentrec0_.agent_id as agent1_1_0_,
txagentrec0_.monday_set as monday2_1_0_,
from
tx_AgentRecordTime txagentrec0_
where
txagentrec0_.agent_id=?
为什么hibernate生成的不是一条简单的SQL语句呢
比如:
select a.*, b.* from tx_account a left join tx_agentrecordtime b on a.account_id = b.agent_id
还是我的映射配置不对,表原来就存在不能修改
------解决思路----------------------
hibernate是默认选择所有列的,sql语句复杂点没关系,不影响什么,主要你是看他查询返回结果是不是你要的