Using OpenJPA
I’ve implemented a simple example using OpenJPA as persistence system. This is my first experience using OpenJPA and I like it, I think that OpenJPA is really a great API. I hope that this post serves as aid to other people who start with OpenJPA.
I started using the “hellojpa” example and the OpenJPA User’s Guide
Writting a simple persistence class:
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_person")
private long idPerson;
@Basic
@Column(nullable = false, length = 30)
private String name;
@Basic
@Column(name = "e_mail")
private String mail;
public Person() {}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getIdPerson() {
return idPerson;
}
public void setIdPerson(long idPerson) {
this.idPerson = idPerson;
}
}
Annotations are very intuitive and powerful.
Writting the persistence.xml I used MySQL as database.
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0">
<persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
<class>example.Person</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema"/>
<property name="openjpa.ConnectionURL"
value="jdbc:mysql://localhost/openjpa"/>
<property name="openjpa.ConnectionDriverName"
value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionUserName"
value="user"/>
<property name="openjpa.ConnectionPassword"
value="password"/>
</properties>
</persistence-unit>
</persistence>
The property “openjpa.jdbc.SynchronizeMappings” is amazing, you don’t need any sql definitions of the tables or the rows, the OpenJPA creates the tables with your object information and if you add a variable it makes an alter automatically.
And finally contructing the EntityManage and saving the object:
EntityManagerFactory factory = Persistence.
createEntityManagerFactory("example", System.getProperties());
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Person person = new Person();
person.setName("Ignacio Andreu");
person.setMail("plunchete AT gmail DOT com");
em.persist(person);
em.getTransaction().commit();
em.close();
And this is all
[...] with JPA Continuing with the JPA Basic Example I’ve written another example with relations between [...]
How do i compile and run this openjpa code ?
hi
can you tell me how to find dirty fields before being flushed using LifecycleListenerS? I am going to build an auditing system in my application. Thanks
Hi Mohammad, have you tried to send and email to the OpenJPA list?
regards,
Great to learn about the property “openjpa.jdbc.SynchronizeMappings”. Thanks!
I have been trying to get an openJPA, WAS7, EJB3 environent working for the past few days now, but have hit a brickwall with ‘inserting’ data.
I have two tables and using the
@Entity
@Table(name = “tbl_user_details”)
@SecondaryTable(name = “tbl_secure_details”, pkJoinColumns = @PrimaryKeyJoinColumn(name = “id”))
But all that happens at after em.persist(dao);
is, the user details table has a new row created but with NULL values (nothing happens to the secure table.
“openjpa.jdbc.SynchronizeMappings”
Something else I noticed, I have this in two persistence-unit as I have two databases for my app, and for some reason it has created all of the tables in database1 in database2!
all the manuals i have seen today said the same, parts of code. but what I have to do to make this work, servers, libraries, and all that things.
sorry, mi english is really bad, son in spanish:
Todos los manuales que he visto hoy dicen lo mismo, tienen solo partes de codigo que ya se que hay que usar pero no me dicen que es lo que hay que instalar ni como, asumen que ya somos expertos, nadie considera a los pobres pavos.
Hey guys!
Do you know (or have an opinion on) whether it makes sense to use OpenJPA for a veeeryy light-weight swing-only client???
Thanks a lot!
Hi Werner,
IMHO frameworks like OpenJPA or Hibernate are not very light-weight. Depens on your project but I really enjoy using frameworks like siena. Siena is light-weight and simple
Cheers!
Wow! This API is amazing! Thanks for the advise on SynchronizeMappings.
And plunchete, I’ll check out Siena. It look promising.