问题描述
我想创建一个将使用Apache Derby Embedded DB的Java独立Java应用程序。
我想使用JPA,因为我对它非常熟悉,可能在Hibernate上也很熟悉(但如果它变得更容易,其他人则欢迎)。 b
$ b
是否有任何示例在Derby嵌入式数据库上使用JPA而不是Hibernate?我在四处搜寻,但找不到任何有用的示例代码。我希望像教程那样能够告诉我需要在我的类路径中添加哪些库,这些是必需的文件以及我需要做的事情。 解决方案
article.htmlrel =noreferrer> http://www.vogella.com/articles/JavaPersistenceAPI/article.html实体: p>
package de.vogella.jpa.simple.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
公共类Todo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
私有长ID;
私人字符串摘要;
私有字符串描述;
public String getSummary(){
return summary;
}
public void setSummary(String summary){
this.summary = summary;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
$ b @Override
public String toString(){
returnTodo [summary =+ summary +,description =+ description
+];
}
}
Persistence.xml
<?xml version =1.0encoding =UTF-8?>
< 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.0xmlns =http://java.sun.com/xml/ns/persistence >
< persistence-unit name =todostransaction-type =RESOURCE_LOCAL>
< class> de.vogella.jpa.simple.model.Todo< / class>
<属性>
< property name =javax.persistence.jdbc.drivervalue =org.apache.derby.jdbc.EmbeddedDriver/>
< property name =javax.persistence.jdbc.url
value =jdbc:derby:/ home / vogella / databases / simpleDb; create = true/>
< property name =javax.persistence.jdbc.uservalue =test/>
< property name =javax.persistence.jdbc.passwordvalue =test/>
< property name =eclipselink.ddl-generationvalue =create-tables/>
< property name =eclipselink.ddl-generation.output-mode
value =database/>
< / properties>
< / persistence-unit>
< /余辉>
客户:
package de.vogella.jpa.simple.main;
import java.util.List;
导入javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
导入de.vogella.jpa.simple.model.Todo;
public class Main {
private static final String PERSISTENCE_UNIT_NAME =todos;
私有静态EntityManagerFactory工厂;
public static void main(String [] args){
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
//读取现有条目并写入控制台
Query q = em.createQuery(从todo t中选择t);
列表< Todo> todoList = q.getResultList();
for(Todo todo:todoList){
System.out.println(todo);
}
System.out.println(Size:+ todoList.size());
//创建新的待办事项
em.getTransaction()。begin();
Todo todo =新Todo();
todo.setSummary(This is a test);
todo.setDescription(This is a test);
em.persist(todo);
em.getTransaction()。commit();
em.close();
}
}
I want to create a Java standalone Java application that will use the Apache Derby Embedded DB.
I want to use JPA since I'm quite familiar with it and probably over Hibernate ( but others are welcome if it is to get easier).
Is there any example that uses JPA over Hibernate over a Derby embedded db? I was searching around but I can't find any useful example code. I would like something like a tutorial that will tell which libs I need to add in my classpath, which are the essential files and what I need to do.
Check the following link for a good example:
http://www.vogella.com/articles/JavaPersistenceAPI/article.html
Entity:
package de.vogella.jpa.simple.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Todo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String summary;
private String description;
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Todo [summary=" + summary + ", description=" + description
+ "]";
}
}
Persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="todos" transaction-type="RESOURCE_LOCAL">
<class>de.vogella.jpa.simple.model.Todo</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:derby:/home/vogella/databases/simpleDb;create=true" />
<property name="javax.persistence.jdbc.user" value="test" />
<property name="javax.persistence.jdbc.password" value="test" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
Client:
package de.vogella.jpa.simple.main;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import de.vogella.jpa.simple.model.Todo;
public class Main {
private static final String PERSISTENCE_UNIT_NAME = "todos";
private static EntityManagerFactory factory;
public static void main(String[] args) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
// Read the existing entries and write to console
Query q = em.createQuery("select t from Todo t");
List<Todo> todoList = q.getResultList();
for (Todo todo : todoList) {
System.out.println(todo);
}
System.out.println("Size: " + todoList.size());
// Create new todo
em.getTransaction().begin();
Todo todo = new Todo();
todo.setSummary("This is a test");
todo.setDescription("This is a test");
em.persist(todo);
em.getTransaction().commit();
em.close();
}
}
这篇关于具有JPA + Hibernate(或类似)和Apache Derby嵌入式数据库的Java独立应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!