简体   繁体   English

Hibernate JPA + OSGi + SQLite,如何?

[英]Hibernate JPA + OSGi + SQLite, how?

I am having problem to integrate the Hibernate JPA with OSGi (Apache Aries) and the SQLite database. 我在将Hibernate JPA与OSGi(Apache Aries)和SQLite数据库集成时遇到问题。 The problem is that the Hibernate is not able to persist the entity class into the table. 问题在于,Hibernate无法将实体类持久化到表中。 There is no error to indicate any problem occurred when persisting the entity class but I don't see the SQL insert statement is generated by the Hibernate which does for Derby database. 没有错误表明持久化实体类时发生任何问题,但是我看不到Hibernate为Derby数据库生成SQL插入语句。

So when I call like this: 所以当我这样打电话时:

People aPeople = new PeopleImpl();
aPeople.setAddress("555 abc st, richmond");
aPeople.setName("AAA Smith");
aPeople.setNumber(555555);

PeoplePersistenceService peoplePersistenceService = 
    peoplePersistenceServiceTracker.getService();

peoplePersistenceService.AddPeople(aPeople);

The aPeople will not be inserted into the table. aPeople将不会插入到表中。

The current configurations I have work for Derby so I assume the major parts should be correct but I might be missing some SQLite specific stuff. 我已经为Derby使用了当前配置,因此我认为主要部分应该正确,但是我可能缺少一些SQLite特定的东西。 I did a lot search but I couldn't find an example for Hibernate JPA AND OSGi AND SQLite. 我进行了大量搜索,但找不到Hibernate JPA和OSGi和SQLite的示例。

I created the SQLite bundle by wrapping the xerial sqlite jdbc v3.7.15-M1. 我通过包装xerial sqlite jdbc v3.7.15-M1创建了SQLite捆绑包。 Also I copied the SQLite dialect code from internet ( http://code.google.com/p/hibernate-sqlite/source/browse/trunk/source/src/main/java/com/applerao/hibernatesqlite/dialect/SQLiteDialect.java ) to build the hibernate dialect. 我也从互联网复制了SQLite方言代码( http://code.google.com/p/hibernate-sqlite/source/browse/trunk/source/src/main/java/com/applerao/hibernatesqlite/dialect/SQLiteDialect。 java )来构建休眠方言。

I really appropriate if someone can show me the correct configurations that work for this case or point me the right track. 如果有人可以向我展示适用于这种情况的正确配置或为我指明正确的道路,那我真的很合适。

The following are my settings, and please let me know if you need any other information. 以下是我的设置,如果您需要其他任何信息,请告诉我。

datasource.xml: datasource.xml:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            default-activation="lazy">

    <bean id="sqliteDataSource" class="org.sqlite.SQLiteDataSource">
        <property name="url" value="jdbc:sqlite:aeServerDatabase.db"/>
    </bean>

    <service ref="sqliteDataSource" interface="javax.sql.DataSource">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="jdbc/sqlite/AeServerDatabase" />
        </service-properties>
    </service>
</blueprint>

persistence.xml: persistence.xml中:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_2_0.xsd"
                version="2.0">

   <persistence-unit name="peopleExampleSQLite" transaction-type="RESOURCE_LOCAL">
       <description>Persistence Unit for the People Persistence Example with SQLite</description>
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <non-jta-data-source>osgi:service/jdbc/sqlite/AeServerDatabase</non-jta-data-source>
       <class>ae.bundles.services.dal.example.PeopleImpl</class>
       <exclude-unlisted-classes>true</exclude-unlisted-classes>

       <properties>
<!--            <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC"/> -->
<!--            <property name="javax.persistence.jdbc.url" value="jdbc:sqlite:aeServerDatabase.db"/> -->
<!--            <property name="hibernate.connection.driver_class" value="org.sqlite.JDBC"/> -->
<!--            <property name="hibernate.connection.url" value="jdbc:sqlite:aeServerDatabase.db"/> -->

<!--             The special SQL dialect for SQLite  -->
           <property name="hibernate.dialect" value="ae.bundles.services.dal.dialect.SQLiteDialect"/>
           <property name="hibernate.hbm2ddl.auto" value="create"/>

           <property name="hibernate.show_sql" value="true"/>
           <property name="hibernate.format_sql" value="true"/>
       </properties>
   </persistence-unit>

</persistence>

The blueprint.xml: blueprint.xml:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0"
    xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.1.0"
    default-activation="lazy">

    <bean id="peoplePersistenceImpl"
        class="ae.bundles.services.dal.example.PeoplePersistenceServiceImpl">
        <tx:transaction method="*" value="Required"/>
        <jpa:context property="entityManager" unitname="peopleExampleSQLite" />
    </bean>

    <service ref="peoplePersistenceImpl"
        interface="ae.bundles.services.dal.example.PeoplePersistenceService" />

</blueprint>

The entity class: 实体类:

package ae.bundles.services.dal.example;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class PeopleImpl implements People
{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private Integer number;
    private String address;

    public void setName(String name) 
    {
        this.name = name;
    }

    public String getName() 
    {
        return name;
    }

    public void setNumber(Integer num) 
    {
        this.number = num;
    }

    public Integer getNumber() 
    {
        return number;
    }

    public void setAddress(String address) 
    {
        this.address = address;
    }

    public String getAddress() 
    {
        return address;
    }

    public Long getId()
    {
        return id;
    }

}

The persistence class: 持久性类:

package ae.bundles.services.dal.example;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;

public class PeoplePersistenceServiceImpl implements PeoplePersistenceService 
{
    private EntityManager em;

    public PeoplePersistenceServiceImpl() {}

    public void setEntityManager(EntityManager em)
    {
        this.em = em;
    }

    public void AddPeople(People p) 
    {
        em.persist(p);
    }

    public People getPeopleByName(String name) 
    {
        String query = "SELECT e FROM PeopleImpl e" +
                " WHERE e.name = '" + name + "'";

        People p = null;

        try
        {
            p = em.createQuery(query, People.class).getSingleResult();
        }
        catch (NoResultException e)
        {
            return null;
        }
        catch (NonUniqueResultException e)
        {
            System.out.println("More than one result available!");
            return null;
        }

        return p;
    }

    public void updatePeopleName(People p, String newName)
    {
        //People target = em.find(PeopleImpl.class, p.getId());
        People target = getPeopleByName(p.getName());

        if (target != null)
        {
            target.setName(newName);
            em.flush();
        }
    }

    public void removePeople(People p) 
    {
        //People target = em.find(PeopleImpl.class, p.getId());
        People target = getPeopleByName(p.getName());

        if (target != null)
        {
            em.remove(target);
            em.flush();
        }
    }

}

Do your get/update/remove methods work? 您的获取/更新/删除方法有效吗? If so, your configuration will be ok and you will simply need to add a flush to your insert method. 如果是这样,您的配置就可以了,您只需要向您的插入方法添加一个刷新。 I have not used hibernate this way, so I cannot speak as to whether or not your configuration is correct. 我没有以这种方式使用休眠模式,因此无法说出您的配置是否正确。 Either way, you should be flushing after the insert if you want this data to be persisted from the first level cache into your database. 无论哪种方式,如果要将此数据从第一级高速缓存持久化到数据库中,都应在插入后进行刷新。 Hope this helps. 希望这可以帮助。

I had the same Problem with AEM cms, it works also with OSGI. 我在AEM cms上遇到了同样的问题,它在OSGI上也起作用。

My anwer is posted here: AEM CQ with JPA (Hibernate) 我的回答发布在这里: AEM CQ with JPA(Hibernate)

I hope it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM