GVKun编程网logo

OSGi +Hibernate

14

对于想了解OSGi+Hibernate的读者,本文将是一篇不可错过的文章,并且为您提供关于Hibernate**关于hibernate4.3版本之后org.hibernate.service.Serv

对于想了解OSGi +Hibernate的读者,本文将是一篇不可错过的文章,并且为您提供关于Hibernate **关于hibernate4.3版本之后org.hibernate.service.ServiceRegistryBuilder被弃用**、Hibernate 4.0中的HibernateDaoSupport、Hibernate annotations VS Hibernate hbm.xml、Hibernate Envers OneToMany Cascade.ALL org.hibernate.HibernateException:找到多个具有给定标识符的行的有价值信息。

本文目录一览:

OSGi +Hibernate

OSGi +Hibernate

而不是将数据库操作分散在四个(osgi)包中,所有操作都在这里稍有不同。我想创建一个负责所有持久性问题的(简单)OSGi捆绑包。我觉得这并不像听起来那么简单,因为“每捆都有唯一的类加载器”。因此,如果有人知道这种问题的解决方案,我将非常感激。

答案1

小编典典

(如果您正在使用hibernate注释)

在通知Hibernate捆绑包有关注释类的信息后,保存所有Entities类加载器。

然后在构建SessionFactory之前执行类似的操作。

ClassLoad cl = Thread.currentThread().getContextClassLoader();try { Thread.currentThread().setContextClassLoader(yourClassLoader); factory = cfg.buildSessionFactory(); }finally { Thread.currentThread().setContextClassLoader(cl);  // restore the original class loader}

Hibernate **关于hibernate4.3版本之后org.hibernate.service.ServiceRegistryBuilder被弃用**

Hibernate **关于hibernate4.3版本之后org.hibernate.service.ServiceRegistryBuilder被弃用**

  • 之前一直都是使用hibernate4.2.21的我,有一天突然没有使用本地的jar包而是让IDEA自动下载最新版本的hibernate5.2.2之后,发现有几个经常使用的方法报错了. 

    //创建配置对象
    Configuration config=new Configuration().configure();
    //创建服务注册对象
    ServiceRegistry serviceRegistry2=new ServiceRegistryBuilder() .applySettings(config.getProperties()).buildServiceRegistry();
    //创建会话工厂对象
    sessionFactory=config.buildSessionFactory(serviceRegistry2);
    //会话对象
    session=sessionFactory.openSession();
    //开启事务
    transaction=session.beginTransaction();

  • -这真是让我惊了个呆,网上一搜,好像有这个问题的人还不少,然后发现果然是hibernate版本的问题——hibernate4.3之后已经没有了org.hibernate.service.ServiceRegistryBuilder这个类,它被弃用了 

     

  • 这下子问题就迎刃而解了 
    -如果使用的是hibernate4.2之前的版本,那么方法就这么写:

        //创建配置对象
        Configuration config = new Configuration().configure();
        //创建服务注册对象
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
        //创建会话工厂对象 sessionFactory = config.buildSessionFactory(serviceRegistry); //会话对象 session = sessionFactory.openSession(); //开启事务 transaction = session.beginTransaction();

 

-如果是hibernate4.3之后的版本,那么方法就这么写: 
导入包更换:org.hibernate.boot.registry.StandardServiceRegistryBuilder;

        //创建配置对象  
        Configuration config = new Configuration().configure();
        //创建服务注册对象  
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config .getProperties()).build();
        //创建会话工厂对象 sessionFactory = config.buildSessionFactory(serviceRegistry); //会话对象 session = sessionFactory.openSession(); //开启事务 transaction = session.beginTransaction();

 

-甚至我还发现到hibernate5之后连上面的包都可以省略了:

        //创建配置对象(读取配置文档)
        Configuration config = new Configuration().configure();
        //创建会话工厂对象
        sessionFactory = config.buildSessionFactory();
        //会话对象
        session = sessionFactory.openSession();
        //开启事务 transaction = session.beginTransaction();

Hibernate 4.0中的HibernateDaoSupport

Hibernate 4.0中的HibernateDaoSupport

我是jsf 2.0 spring 3.1和hibernate 4.1集成的新手。我如何更改以下代码,因为hibernate
4.0不包含HibernateDaoSupport。

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;    public class CustomerDaoImpl extends            HibernateDaoSupport implements CustomerDao{        public void addCustomer(Customer customer){            customer.setCreatedDate(new Date());            getHibernateTemplate().save(customer);        }        public List<Customer> findAllCustomer(){            return getHibernateTemplate().find("from Customer");        }    }

我正在尝试此示例:http : //www.mkyong.com/jsf2/jsf-2-0-spring-hibernate-
integration-example/

答案1

小编典典

我找到了解决方案。我应该改用会话工厂。

import java.util.List;import org.hibernate.SessionFactory;public class CustomerDaoImpl implements CustomerDao{    private SessionFactory sessionFactory;    public SessionFactory getSessionFactory() {        return sessionFactory;}    public void setSessionFactory(SessionFactory sessionFactory) {         this.sessionFactory = sessionFactory;    }    public void addCustomer(Customer customer){        getSessionFactory().getCurrentSession().save(customer);    }    public List<Customer> findAllCustomer(){        List list = getSessionFactory().getCurrentSession().createQuery("from Customer").list();        return list;    }}

Hibernate annotations VS Hibernate hbm.xml

Hibernate annotations VS Hibernate hbm.xml

Hibernate 的 annotations 基本上都是使用 JPA 的规范.

请问你们在项目中更倾向于使用 Annotations 还是 hbm.xml ? 

Hibernate Envers OneToMany Cascade.ALL org.hibernate.HibernateException:找到多个具有给定标识符的行

Hibernate Envers OneToMany Cascade.ALL org.hibernate.HibernateException:找到多个具有给定标识符的行

好的,问题是当你尝试

Car car = new Car();
car.setBrand("Alfa Romeo");
Set<Car> cars = new HashSet<>();
cars.add(car);

Driver driver = new Driver();
driver.setCars(cars);

em.persist(driver);

单向一对多,hibernate执行3条sql语句:

  1. 插入驱动程序 (id) 值 (?)
  2. 插入汽车(品牌,id)值(?,?)
  3. 更新汽车设置 driver_id=?其中 id=?

切换到双向关系

@Entity
@Audited
public class Car {

    @Id
    @GeneratedValue
    private Long id;

    private String brand;

    @ManyToOne
    private Driver driver;

    // Constructor,getters and setters omitted
}

@Entity
@Audited
public class Driver {

    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(cascade = CascadeType.ALL,mappedBy = "driver")
    @AuditJoinTable(name = "car_aud")
    private Set<Car> cars = new HashSet();

    public void setCars(Set<Car> cars) {
        books.forEach(b -> b.setDriver(this));
        this.cars.addAll(cars);
    }

    // Constructor,getters and setters omitted

}

使 Hibernate 只创建 2 个 SQL 语句:

  1. 插入驱动程序 (id) 值 (?)
  2. 插入汽车 (driver_id,brand,id) 值 (?,?,?)

结果相同。所以CAR_AUD表中不会有歧义。

关于OSGi +Hibernate的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Hibernate **关于hibernate4.3版本之后org.hibernate.service.ServiceRegistryBuilder被弃用**、Hibernate 4.0中的HibernateDaoSupport、Hibernate annotations VS Hibernate hbm.xml、Hibernate Envers OneToMany Cascade.ALL org.hibernate.HibernateException:找到多个具有给定标识符的行等相关知识的信息别忘了在本站进行查找喔。

本文标签: