当前位置:首页>开发>正文

hibernate中namespace有什么用 hibernate get方法怎么用

2023-04-12 13:45:55 互联网 未知 开发

 hibernate中namespace有什么用 hibernate get方法怎么用

hibernate中namespace有什么用

使用user namespace(用户命名空间)的例子:

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"url/hibernate-mapping-3.0.dtd" [

]>




...


&types

hibernate get方法怎么用

首先你必须已经正确实现了持久化类User(假定名)和映射文件。。。
get方法是session类的静态方法,有三个重载版本,最常用的还是
public Object get(Class clazz,Serializable id),get方法并不是用来查询的,它一般被用来加载某个持久化对象。如果进行功能强大的查询,还是用HQL吧
get的使用很简单,以楼主的情况为例。假设你的User表中有一条记录,主键值为“username”那么用下面语句即可获得这条记录对应的持久化对象:
User user=new User()
user=(User)session.get(User.class,"username")

唯一要注意的是参数id指的User类的持久化标识符,在映射文件中一般是和数据库表主键相对应的,所以实际情况下也就是字段的主键值。

Hibernate怎么释放连接

hibernate 中连接释放的策略hibernate. connection. release_ mode有一下四种属性:
default : 默认方式
on_close : session关闭时释放连接。

after_transcation : 事务处理结束后,以事务为单位进行连接的释放

after_statement : 每次执行后就释放当前连接。

四种释放连接的颗粒度,从粗到细:

采用第一种default配置,在spring中配置事务管理,由于事务颗粒度比较小,事务执行结束,也不会触发释放的操作,直至达到连接设置回收的最大超时时间才能回收连接,连接会迟迟不释放,导致连接池被占满。
采用第二种on_close, 同样在spring中配置事务,连接一直等到session 关闭时才会被释放,释放较慢,同样会导致连接池被沾满
采用after_transcation 的策略释放链接,每次事务都会释放链接。采用xml配置进行全局事务管理的配置,则不会出现连接池沾满的现象。但是如果采用注解,而某个持久层的 Dao类未标注Transactional注解,或者xml配置中遗漏了某个dao的事务管理配置,则该Dao操作执行结束,并不是一个事务的结束,不会释放链接,导致链接迟迟不能被释放,久而久之会导致连接池被占满。
after_statement 的策略释放连接及时。 但也有一个弊端,由于每一次执行都会释放连接,如果一个事务需要几个执行操作,但第一次执行时连接就被释放,连接已归还给连接池了,第二次执行时获取新的连接,这样就无法保证事务性了。

如何在Eclipse上安装和使用hibernate Tool

打开Eclipse 开发工具
菜单 help,选择 Eclipse marketplace

选择search 选项卡,搜索 hibernate 关键字

选择最新的安装版本(eclipse version)的Hibernate Tools点击install ,弹出选择界面。confirm确认安装。配置使用hibernate Tools,选择 window 菜单中的Customize Perspective,选择 hibernate code generation,将在Eclipse界面工具栏生成命令按钮,点击,选择Hibernate code generation Configuration,new 一个新的配置。可以配置,生成java code 文件等。

hibernate 怎么解决hql里面on的办法

设置Classes中对Student的导航,就可以不用设置on 具体的做法是在Classes中添加一个成员变量: Set students=new HashSet() 在上面加上注解: @OneToMany(cascade=CascadeType.ALL,mappedBy="class") 在Student中添加一个成员变量

hibernate Query方法

/**
* 添加
*/
public void save(Stu stu){
try {
tran=this.GetSession().beginTransaction()
this.GetSession().save(stu)
tran.commit()
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
}

/**
* 使用HQL全查询
*/
public List getallbyHQL(){
List arr=null
try {
String hql="from Stu"
Query query=this.GetSession().createQuery(hql)
arr=query.list()
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
return arr
}
/**
* 根据主键查询
*/
public Stu getbyID(int id){
Stu stu=null
try {
stu=(Stu) this.GetSession().get(Stu.class, id)
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
return stu
}

/**
* 根据对象属性查询(使用Query)
*/
public List getbyPropertyQuery(String name){
List arr=null
try {
//这里不能像SQL语一样select * from Stu where SName=:name,这是不对的。
// Query query=this.GetSession().createQuery("from Stu where SName=:name")
// query.setString("name", name)
//或者
Query query=this.GetSession().createQuery("from Stu where SName=?")
query.setString(0, name)
arr=query.list()
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
return arr
}

/**
* 根据对象属性查询(使用Criteria)
*/
public List getbyPropertyCriteria(String name){
List arr=null
try {
Criteria cri=this.GetSession().createCriteria(Stu.class)
Criterion c1=Expression.eq("SName", name)
cri.add(c1)
arr=cri.list()
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
return arr
}

/**
* 查询部分属性
*/
public List getProperty(){
List arr=new ArrayList()
try {
String hql="select s.SName,s.SSex from Stu as s"
Query query=this.GetSession().createQuery(hql)
List list=query.list()
Iterator iter=list.iterator()
while(iter.hasNext()){
Object[] obj=(Object[]) iter.next()
Stu s=new Stu()
s.setSName(obj[0].toString())
s.setSSex(obj[1].toString())
arr.add(s)
}
} catch (HibernateException e) {
this.CloseSession()
}
return arr
}
/**
* 查询一个属性
*/
public List getoneProperty(){
List arr=new ArrayList()
try {
String hql="select s.SName from Stu as s"
Query query=this.GetSession().createQuery(hql)
Iterator iter=query.iterate()
while(iter.hasNext()){
Object obj=(Object) iter.next()
Stu s=new Stu()
s.setSName(obj.toString())
arr.add(s)
}
} catch (HibernateException e) {
this.CloseSession()
}
return arr
}

/**
*查询一个对象一个属性值
*/
public Object getonlyProprotyValue(int s_id){
Object obj=null
try {
String hql="select s.SName from Stu as s where s.SId=?"
Query query=this.GetSession().createQuery(hql)
query.setInteger(0, s_id)
obj=query.uniqueResult()
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
return obj
}
/**
* SQL查询
*/
public List getallBYSQL(){
List arr=null
try {
String sql="select {c.*} from stu as c"
SQLQuery sqlquery=this.GetSession().createSQLQuery(sql)
sqlquery.addEntity("c",Stu.class)
arr=sqlquery.list()
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
return arr
}

/**
* 根据对象查询
*/
public List getallByObject(Stu s){
List arr=null
try {
String hql="from Stu as s where s=:stuentity"
//或者
//String hql="from Stu as s where s.SId=:stuentity"
Query query=this.GetSession().createQuery(hql)
query.setEntity("stuentity", s)
arr=query.list()
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
return arr
}

/**
* 模糊查询
*/
public List getallQueryLike(String name){
List arr=null
try {
String hql="from Stu as s where s.SName like :name"
Query query=this.GetSession().createQuery(hql)
query.setString("name", "%" name "%")
//不能
//query.setString("name", "%" name "%")
arr=query.list()
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
return arr
}
/**
* 统计函数
*/
public int CountStu(){
int count=0
try {
String hql="select count(*) from Stu"
Query query=this.GetSession().createQuery(hql)
count=(Integer) query.uniqueResult()
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
return count
}

/**
* 条件统计
*/
public int CountByWhere(String sex){
int count=0
try {
Query query=this.GetSession().createQuery("select count(*) from Stu where SSex=:sex")
query.setString("sex", sex)
count=(Integer)query.uniqueResult()
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
return count
}

/**
* 统计平均值
*/
public float VagAge(){
float vag=0
try {
Query query=this.GetSession().createQuery("select avg(SAge) from Stu")
vag=(Float)query.uniqueResult()
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
return vag
}

/**
* 求和函数
*/
public int sumage(){
int sum=0
try {
Query query=this.GetSession().createQuery("select sum(SAge) from Stu")
sum=(Integer)query.uniqueResult()
} catch (HibernateException e) {
throw e
}finally{
this.CloseSession()
}
return sum
}

最新文章