<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="org.hibernate.tutorial.jpa">
<description>
Persistence unit for the JPA tutorial of the Hibernate Getting Started Guide
</description>
<class>com.test.Cat</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:postgresql://192.168.3.1:5432/testdb" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
CatTest.java
package com.test;
import static org.junit.Assert.*;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.LockModeType;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class CatTest {
static EntityManagerFactory entityManagerFactory;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
entityManagerFactory = Persistence
.createEntityManagerFactory("org.hibernate.tutorial.jpa");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
entityManagerFactory.close();
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void test01() {
EntityManager entityManager = entityManagerFactory
.createEntityManager();
entityManager.getTransaction().begin();
Cat cat1 = new Cat();
cat1.setName("Tom");
entityManager.persist(cat1);
Cat cat2 = new Cat();
cat2.setName("Jerry");
cat2.setAddress("newyork");
entityManager.persist(cat2);
entityManager.getTransaction().commit();
}
@Test
public void test02() {
EntityManager entityManager = entityManagerFactory
.createEntityManager();
entityManager.getTransaction().begin();
try {
Cat cat1 = entityManager.find(Cat.class, 1);
System.out.println(cat1.getName());
Cat cat2 = entityManager.find(Cat.class, 2,
LockModeType.PESSIMISTIC_READ);
System.out.println(cat2.getName());
cat2.setName("Jerry new");
entityManager.flush();
entityManager.getTransaction().commit();
}
catch (Exception e) {
entityManager.getTransaction().rollback();
e.printStackTrace();
}
}
@Test
public void test03() {
EntityManager entityManager = entityManagerFactory
.createEntityManager();
entityManager.getTransaction().begin();
try {
List<Cat> list = entityManager.createQuery("from Cat order by id ",
Cat.class).getResultList();
for (Cat cat : list) {
System.out.println(cat.getName());
}
entityManager.getTransaction().commit();
}
catch (Exception e) {
entityManager.getTransaction().rollback();
}
}
}
Cat.javapackage com.test;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Cat {
private int id;
private String name;
private String address;
private String telphone;
@Column(name = "tphone")
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
0 件のコメント:
コメントを投稿