JPA初歩
注意:例は「Hibernate In Action」に必要なjarパッケージに由来します.hibernateに必要な最小jarパッケージにejb 3-persistence.jarとhibernate-annotation.jarを追加するには、まずエンティティクラスMessage.javaを構築し、Annotationを使用してエンティティのマッピング関係を構成します.
次に、srcディレクトリの下にMETA-INFフォルダを作成し、MEAT-INFにpersistence.xmlファイルを新規作成します.
最後にテストクラスを作成します.
package hello;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "MESSAGES")
public class Message {
@Id @GeneratedValue
@Column(name = "MESSAGE_ID")
private Long id;
@Column(name = "MESSAGE_TEXT")
private String text;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "NEXT_MESSAGE_ID")
private Message nextMessage;
public Message(){}
public Message(String text) {
this.text = text;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Message getNextMessage() {
return nextMessage;
}
public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}
}
次に、srcディレクトリの下にMETA-INFフォルダを作成し、MEAT-INFに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_1_0.xsd"
version="1.0">
<persistence-unit name="helloworld">
<!--
<properties>
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>
</properties>
-->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- Not needed,Hibernate supports auto-dection in JSE
<class>hello.Message</class>
-->
<properties>
<!-- Auto detection -->
<property name="hibernate.archive.autodetection" value="class,hbm"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/hibernateEM"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<!-- Use the C3P0 connection pool provider -->
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="50"/>
</properties>
</persistence-unit>
</persistence>
最後にテストクラスを作成します.
package hello;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
//start EntityManagerFactory
EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld");
//First unit of work
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Message message = new Message("Hello World");
em.persist(message);
tx.commit();
em.close();
//Second unit of work
EntityManager newEm = emf.createEntityManager();
EntityTransaction newTx = newEm.getTransaction();
newTx.begin();
List messages = newEm.createQuery("select m from Message m order by m.text asc").getResultList();
System.out.println(messages.size() + "messages(s) found");
for(Object m : messages) {
Message loadedMsg = (Message)m;
System.out.println(loadedMsg.getText());
}
newTx.commit();
newEm.close();
//Shutting down the application
emf.close();
}
}