i want to use hibernate with embedded derby in a standalone application, and i have some questions:
if you can also suggest me some good tutorial for this approach that will be preferable, thanks in advance.
If you want to connect to the Derby database in embedded mode you can use the following command. In this example the database is located at c:\temp\db\FAQ\db. connect 'jdbc:derby:c:\temp\db\FAQ\db'; If you want to connect to a Derby database which is running in server mode then you can use the following command.
To include derby into the project, All you have to do is to add the Derby and JPA Starter dependencies from Spring Boot.
To install Apache Derby in embedded mode, include the jar file derby. jar in your CLASSPATH. After setting up Apache Derby, to access it, run Java programs using the embedded driver.
I use Apache Derby with Hibernate for testing one of my project's model classes (their equals, hashCode implementations, queries, etc.). MySQL is used as the production database. I choose Derby instead of HSQLDB, because I've experienced some incompatibilities with Hibernate and HSQLDB, meaning, given my entities (their name, schema, keys) and their relation Hibernate couldn't create my database schema in HSQLDB, while it could with Derby. That said, maybe I messed up something; also the incompatibilities could have been resolved.
Anyway, here is what I use in my tests (I've modified my pom.xml
so that it would include Derby as a runtime dependency and run a single test, just to make sure it's working).
pom.xml
<dependencies>
...
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.8.Final</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.8.2.2</version>
<scope>runtime</scope>
</dependency>
<!--
an slf4j implementation is needed by
hibernate so that it could log its *stuff*
-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
<scope>runtime</scope>
</dependency>
...
</dependencies>
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 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="test">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Test</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<!--
if you don't have a database already created
append ;create=true to end of the jdbc url
-->
<property name="javax.persistence.jdbc.url" value="jdbc:derby:test"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<!--
if you just created the database, maybe
you want hibernate to create a schema for you
<property name="hibernate.hbm2ddl.auto" value="create"/>
-->
</properties>
</persistence-unit>
</persistence>
Test
entity@Entity
@Table(name = "test")
public class Test {
@Id
public int id;
@Basic
public String data;
}
EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
Test test = em.find(Test.class, 1);
if (test == null) {
test = new Test();
test.id = 1;
test.data = "a";
tx.begin();
em.persist(test);
tx.commit();
}
System.out.format("Test{id=%s, data=%s}\n", test.id, test.data);
em.close();
emf.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With