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
May 15, 2007 at 11:13 pm |
[...] with JPA Continuing with the JPA Basic Example I’ve written another example with relations between [...]
January 7, 2008 at 8:26 pm |
How do i compile and run this openjpa code ?
October 17, 2008 at 7:32 pm |
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
October 21, 2008 at 11:32 am |
Hi Mohammad, have you tried to send and email to the OpenJPA list?
regards,
October 20, 2009 at 4:41 am |
Great to learn about the property “openjpa.jdbc.SynchronizeMappings”. Thanks!
December 4, 2009 at 3:57 pm |
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.
December 4, 2009 at 5:02 pm |
“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!