Hibernate JPA + OSGi + SQLite, how? -
i having problem integrate hibernate jpa osgi (apache aries) , sqlite database. problem hibernate not able persist entity class table. there no error indicate problem occurred when persisting entity class don't see sql insert statement generated hibernate derby database.
so when call 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 not inserted table.
the current configurations have work derby assume major parts should correct might missing sqlite specific stuff. did lot search couldn't find example hibernate jpa , osgi , sqlite.
i created sqlite bundle wrapping xerial sqlite jdbc v3.7.15-m1. copied sqlite dialect code internet (http://code.google.com/p/hibernate-sqlite/source/browse/trunk/source/src/main/java/com/applerao/hibernatesqlite/dialect/sqlitedialect.java) build hibernate dialect.
i appropriate if can show me correct configurations work case or point me right track.
the following settings, , please let me know if need other information.
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:
<?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 people persistence example 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"/> --> <!-- special sql dialect 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 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 peopleimpl e" + " 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 1 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 get/update/remove methods work? if so, configuration ok , need add flush insert method. have not used hibernate way, cannot speak whether or not configuration correct. either way, should flushing after insert if want data persisted first level cache database. hope helps.
Comments
Post a Comment