check out all of the fields in Table B
check out the salary of Table C
The three tables are related by ID
A table's primary key is the ID
B table's primary key is the ID and modification time
C table's primary key is the ID number of changes and modification time and
seeking to write a HQL statement ?
I do not understand where a few big God seeking guidance . That makes sense to the sub-
multi- table associated with the time when the hbm.xml and mostly associated with the primary key should be how to deal with it , pay attention to where
can be directly used in Table A ID many-to-one the other two tables ID number ?
hql query result how to receive it .
received this object should check out the three fields in the table plus all the new class with new what ?
sense that give points thanks
------ Solution ------------------------------------ --------
refer to the following statement now , in general , and SQL similar
select
student0_.id as id1_0_,
course2_.id as id4_1_,
student0_.Sno as Sno1_0_,
student0_.Sname as Sname1_0_,
student0_.Sdept as Sdept1_0_,
student0_.Sage as Sage1_0_,
student0_.Ssex as Ssex1_0_,
student0_.Saddress as Saddress1_0_,
course2_.Cno as Cno4_1_,
course2_.Cname as Cname4_1_,
course2_.Ccredit as Ccredit4_1_
from
joblog.student student0_
left outer join
sc course1_
on student0_.id=course1_.sno
left outer join
joblog.course course2_
on course1_.cno=course2_.id where
student0_.Sname='李XXX'
------ For reference only ----------------------------------- ----
you just configured a table and the other relationship between two tables , and then directly query a table you can get all the data associated . Do not you think so complicated, you should not understand the role of hibernate , I suggest you do find an example of what is easier to understand
------ For reference only ---------- -----------------------------
use of joint crawl , if three tables are linked
such as A B C, B contains A, C, B-containing foreign key foreign key
hql example : from C c join fech c.bb b join fetch b.aa a where a.id = 1
If the result is a record , then use C c to receive
If it is more than one , then use the List
------ For reference only ---------------------------------- -----
<class name="" table="busin_name" >a small example , the primary key wording
<composite-id name="comp_id" class="po的名称">
<key-property name="id" column="id" type="java.lang.Long" length="10" />
<key-property name="business_id" column="business_id" type="java.lang.Long" length="10" />
</composite-id>
<many-to-one name="a" class="a" update="false" insert="false" >
<column name="id " />
</many-to-one>
<many-to-one name="b" class="b" update="false" insert="false" >
<column name="business_ID" />
</many-to-one>
</class>
------ For reference only ------------------- --------------------
HQL should ask how to write it . Query a table of all the data and some fields in another table
------ For reference only ---------------------------------- -----
query part of the field , then you can select * from table and select from the list between the fields you want to you can
------ For reference only ---------------------------------------
Hibernate ; HQL query some fields POJO
When using Hibernate , sometimes do not want the object to check out all fields , especially in the data object in the database is large, ( eg store some pictures or binary file, because of the special business needs , must do exist ) , the use of objects, but not with these lob fields , you do not want to check out these fields together , you can add domain DOMAIN object in the constructor of the field you want to search , and then use the select new hql statement ; Object ( construction field ) from Object where conditions ;
Note that in the query, added after the from from User as u, in the constructor to use u.name, u. etc. . Conditional statement is .
public class User () {
String id;
String name;
String age;
String image;
public User (String id, String name, String age) {
this.id = id;
...
}
}
if you only want part of the query field , HQL statement read:
String hql = "select new User (u.id, u.name, u.age) from User as u where u.id = ? " ;
------ For reference only ---------------------------------- -----
I understand what you mean , there is a place not very clear if it is a multi- table
select new (A.ID, A.NAME, B.salary
c.xxx, c.xxxx ...) ....
pojo is not trying to write a new class which includes all the fields all queries it
can only write so what ?
------ For reference only -------------------------------------- -
not that people write hql do
------ For reference only ----------------------- ----------------
using native SQL okay
------ For reference only -------------- -------------------------
This statement is the hql check out A, BC all the data table yet ?
------ For reference only -------------------------------------- -
long time not visiting the forum , and a more optimistic about the problem . .
If you want to achieve the kind of requirement, then I feel inside hql write directly on the easiest on the line, such as the following example
//当不是查询全字段,或者是从两张表中联合查询数据时,返回的是一个数组:
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery("select a.id,b.title,b.username from A as a,B b");
//这里的A,B依然是对象
List list = query.list();//这里每一行都是一个1维数组
for(int i=0;i<list.size();i++)
{
Object []o = (Object[])list.get(i); //转型为数组
int id = (Integer)o[0]; //和select中顺序的类型相对应,可以是类
String title = (String)o[1];
String username = (String)o[2];
System.out.println("id:"+id+" , "+"title"+title+" , "+username);
}
HibernateSessionFactory.closeSession();
query result set size
(Integer) session.createQuery ("select count (*) from User"). iterate (). next ();
do not know if help you, if or not , I am afraid the old lady helpless. . . .
------ For reference only -------------------------------------- -
If it is not, try session.createSQLQuery ("sql statement ")
没有评论:
发表评论