delete from employees where employeeId is null; //注意,不能是=null, 必须用is。
select employees.employeeId, employees.first_name, location.location from employees left [outer] join location on employees.employeeId = location.employeeId order by location desc; //注意,左连接就是从左面连接,右连接就是从右面接入,左边的全显示,右面的只显示复合条件的。 Innerjoin则只显示共有的,如果有一对多或者多对一,则予以复制。
select employees.employeeId, employees.first_name, location.location from employees right join location on employees.employeeId = location.employeeId;
select employees.employeeId, employees.first_name, location.location from employees inner join location on employees.employeeId = location.employeeId;
select first_name from employees where employeeid = (select employeeid from locations where location = 'montreal')
select * from employees where employeeId in (select employeeId from locations) //跟inner Join类似,但不会像Inner join 显示重复行。any 和all要结合运算符使用。例如:
多行子查询使用ANY操作符号例子:查询有一门以上的成绩高于Kaka的任何一门成绩的学生的名字: select stName from Student where stId in(select distinct stId from score where score >any(select score from score where stId=(select stId from Student where stName='Kaka')))
select distinct employeeid from locations;
SELECT first_name FROM employees WHERE family_name LIKE 'ha_' or family_name like 'j%'
select first_name from employees where family_name like 'ha_' or family_name like '_i[^d-f]%'; //不知怎么[在psQL上通过不了]
select * from employees where first_name like 'tab%%' escape 'b' //我把名字改成了ta%o, 这里的b可以用(几乎)任意字符替代
select first_name from employees where (family_name, employeeId) in (select family_name, max(employeeId) from employees where worktime = 12 group by family_name)
选出所有薪水比其经理高的员工:select * from employees as e1 join employees as e2 on e1.reportto = e2.employeeid where e1.salary > (select salary from employees where employeeId = e1.reportto);
运算符:max() min() count() avg() <> < > <= >= AND OR NOT AND IS NULL IS NOT NULL; //NULL等于NULL,但null和其他值相比,结果为unknown
----always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one transaction after another one, means wont process one transaction many times. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction.
use flush and clear to avoid outofmemory issue.
二级缓存:
when do the select * from table where.... hibernate save the result into second cache, then when use do query base on ID, hibernate will search the first cache, then the second level cache,, then the database, if the database is changed, hibernate will change the second level cache.
SessionFactory : initialize hibernate and be the proxy of data source, and generate session.
Transaction : an abstraction of real transaction, the real transaction can be a UserTransaction in JPA, a JDBC transaction or even a corba transaction.
there are more than 10 different ways to generate the main key in hibernate. uuid.hex is faster than self increasing, because for self increasing, every time you insert a new record, it need to read and write database several times. 读目前最大值,读递增跨度,写新的最大值,然后才是写new记录
if don't need to display on page, can save the limitation like @manyToOne, just a foreign key is OK.
Contract表中有个ManyToOne fiele: Employee, use this way to save one time of query:
Open the show_sql Flag in hibernate.cfg.xml, so we can check the sql generated in control, (如果查询次数和数据量有关,则视作一个潜在的大bug)
Adjust the hibernate.jdbc.fetch_size and hibernate.jdbc.batch_size
make it update only the modified fields. @org.hibernate.annotations.Entity(dynamicUpdate=true,dynamicInsert=true)
set the cach of hibernate.
use lazyload (while, for jpa, @OneToMany is bydefault lazyload true, @ManyToOne is bydefault lazyload false. but some one said hibernate will change this behavior, need to check in future.
move some initialization into startup.(like what?)
normally 选择version方式作为Hibernate乐观锁.
manage the session in ThreadLocal mode to avoid the data sharing error between thread.