想了解Hibernate+MSSQL+通过包含进行全文本搜索的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于Hibernate-无法提交Hibernate事务;嵌套异常是org.hib
想了解Hibernate + MSSQL +通过包含进行全文本搜索的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于Hibernate - 无法提交 Hibernate 事务;嵌套异常是 org.hibernate.TransactionException、Hibernate -- hibernate.cfg.xml 核心配置文件、Hibernate 1、Hello Hibernate、Hibernate 4 升级到 Hibernate 5 的时候 SessionFactory 不能使用的新知识。
本文目录一览:- Hibernate + MSSQL +通过包含进行全文本搜索
- Hibernate - 无法提交 Hibernate 事务;嵌套异常是 org.hibernate.TransactionException
- Hibernate -- hibernate.cfg.xml 核心配置文件
- Hibernate 1、Hello Hibernate
- Hibernate 4 升级到 Hibernate 5 的时候 SessionFactory 不能使用
Hibernate + MSSQL +通过包含进行全文本搜索
我的目标是将MSSQL全文功能与HQL一起使用。在哪种情况下,我编写了一个特定的SQLFunction,将我的“全文”函数映射到contains函数。
但是,问题在于,在HQL中(似乎)我必须明确使用返回类型,MSSQL Contains函数不使用或不接受该返回类型。
这就是它在MSSQL中的工作方式:
select distinct id from content c where CONTAINS(c.content,'p')
这是我在HQL中使用它的想法:
select id from Content c where fulltext(c.content,'p')
这不起作用,因为HQL需要返回类型。例如,这将在HQL中解析:
select id from Content c where fulltext(c.content,'p') = true
它将生成为SQL:
select distinct id from content c where CONTAINS(c.content,'p') = 1
在MS SQL中不起作用。
到目前为止,我的想法是,但是在这种设置下似乎是不可能的:
- 使hibernate解析函数没有返回值(在我使用的版本中,Hibernate不支持此功能)
- 尝试混合使用HQL和SQL(似乎也不起作用)
有人有其他想法或帮助吗?
我使用的Hibernate版本是3.2.6ga和MSSQL Server 2008。
Hibernate - 无法提交 Hibernate 事务;嵌套异常是 org.hibernate.TransactionException
如何解决Hibernate - 无法提交 Hibernate 事务;嵌套异常是 org.hibernate.TransactionException
我需要从两个不同的数据库中获取数据。为此,我创建了两个会话、两个事务管理器。我正在从第一个事务方法调用第二个事务方法。它在一段时间内工作正常,但之后由于某种原因,hibernate 抛出以下异常。谁能帮我?提前致谢。
#include<stdlib.h>
/*
{
"name":"name1","city":"city1","phone":"125698745663"
}
/* Boxes of my list */
typedef_struct Box
{
char* current_string;
Box* next;
}Box;
/* Define the structure of the list that I''ll return after*/
typedef_struct list
{
Box* beginning;
}list;
/*The function that return the list of the objects that existed in our files*/
**char lire (FILE* my_file) //It is possibly to put the path of the file as an argument
{
char* nameFile;
printf("enter the name of the file");
scanf("%s",&nameFile);
my_file = fopen( "nameFile","r");
if (my_file != NULL)
{
Box* temp;
int i=0;
list* my_list;
int first_time = 0;
/* browse file elements */
do
{
char c = fgetc(my_file);
/* I put the following condition to get the value without its key*/
if(c == ":")
{
while(c!=",")
{
temp->current_string[i] = c;
i++;
// printf("%c",c);
c=fgetc(my_file);
}
}
/*I put this condition to pass to the following element of the list and fill it*/
if(c == "}")
{
first_time++;
/*This condition is for getting the begining of the list because it is what distinguishes the list*/
if(first_time==1)
my_list->beginning->current_string = temp->current_string;
temp = temp->next;
}
} while (c != EOF);
fclose(my_file);
}
return my_list;
}```
Hibernate -- hibernate.cfg.xml 核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- property 元素用于配置Hibernate中的属性
键:值
-->
<!-- hibernate.connection.driver_class : 连接数据库的驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- hibernate.connection.username : 连接数据库的用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- hibernate.connection.password : 连接数据库的密码 -->
<property name="hibernate.connection.password">123</property>
<!-- hibernate.connection.url : 连接数据库的地址,路径 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedem</property>
<!-------------------------------------------------------------------------------->
<!-- show_sql: 操作数据库时,会 向控制台打印sql语句 -->
<property name="show_sql">true</property>
<!-------------------------------------------------------------------------------->
<!-- format_sql: 打印sql语句前,会将sql语句先格式化 -->
<property name="format_sql">true</property>
<!-------------------------------------------------------------------------------->
<!-- hbm2ddl.auto: 生成表结构的策略配置
update(最常用的取值): 如果当前数据库中不存在表结构,那么自动创建表结构.
如果存在表结构,并且表结构与实体一致,那么不做修改
如果存在表结构,并且表结构与实体不一致,那么会修改表结构.会保留原有列.
create(很少):无论是否存在表结构.每次启动Hibernate都会重新创建表结构.(数据会丢失)
create-drop(极少): 无论是否存在表结构.每次启动Hibernate都会重新创建表结构.每次Hibernate运行结束时,删除表结构.
validate(很少):不会自动创建表结构.也不会自动维护表结构.Hibernate只校验表结构. 如果表结构不一致将会抛出异常.
-->
<property name="hbm2ddl.auto">update</property>
<!-------------------------------------------------------------------------------->
<!-- 数据库方言配置
org.hibernate.dialect.MySQLDialect (选择最短的)
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-------------------------------------------------------------------------------->
<!-- hibernate.connection.autocommit: 事务自动提交 -->
<property name="hibernate.connection.autocommit">true</property>
<!-------------------------------------------------------------------------------->
<!-- 将Session与线程绑定=> 只有配置了该配置,才能使用getCurrentSession -->
<property name="hibernate.current_session_context_class">thread</property>
<!-------------------------------------------------------------------------------->
<!-- 引入ORM 映射文件
填写src之后的路径
-->
<mapping resource="com/itheima/a_hello/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate 1、Hello Hibernate
所使用到的jar 包:
1、创建实体类
public class User {
private Integer id;
private String name;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2、配置映射文件
<hibernate-mapping>
<class name="cn.model.User" table="USER" lazy="true">
<id name="id" column="ID">
<generator></generator>
</id>
<property name="name" column="NAME" type="java.lang.String" />
<property name="address" column="ADDRESS" type="java.lang.String" />
</class>
</hibernate-mapping>
3、配置hiberante.cfg.xml;hibernate.properties 详细 http://blog.csdn.net/oxiaoxio/article/details/49304591
<hibernate-configuration>
<session-factory name="sessionFactory">
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="cn/hbm/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、测试用例:
public class Conn {
public void addUser(){
Configuration cfg=new Configuration();
cfg.configure();
ServiceRegistry sr=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf=cfg.buildSessionFactory(sr);
Session s=sf.openSession();
Transaction trans=s.beginTransaction();
User user=new User();
user.setName("汤姆");
user.setAddress("北海");
s.save(user);
trans.commit();
s.close();
}
}
代码优化:
新建一个类名字是HibernateSessionFactory:
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateSessionFactory {
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static org.hibernate.SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
private static ServiceRegistry serviceRegistry;
static {
try {
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Configuration getConfiguration() {
return configuration;
}
}
优化过以后代码:
public void addUser(){
Session s=HibernateSessionFactory.getSession();
Transaction trans=s.beginTransaction();
User user=new User();
user.setName("汤姆11");
user.setAddress("北海");
s.save(user);
trans.commit();
s.close();
}
Hibernate 4 升级到 Hibernate 5 的时候 SessionFactory 不能使用
在 Hibernate 4 升级到 5 的时候老的 sessionFactory 出现错误。
public static SessionFactory initSession() { Configuration configuration = new Configuration(); configuration.configure(); serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); return sessionFactory; }
你可以使用下面的代码来在 Hibernate 5 中初始化:
public static SessionFactory initSession() { Configuration configuration = new Configuration(); configuration.configure(); serviceRegistry = new StandardServiceRegistryBuilder().configure().build(); Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build(); sessionFactory = metadata.getSessionFactoryBuilder().build(); return sessionFactory; }
今天关于Hibernate + MSSQL +通过包含进行全文本搜索的介绍到此结束,谢谢您的阅读,有关Hibernate - 无法提交 Hibernate 事务;嵌套异常是 org.hibernate.TransactionException、Hibernate -- hibernate.cfg.xml 核心配置文件、Hibernate 1、Hello Hibernate、Hibernate 4 升级到 Hibernate 5 的时候 SessionFactory 不能使用等更多相关知识的信息可以在本站进行查询。
本文标签: