JPA中实体类的一些配置
一.单表增删改查
(1)实体类
@Entity
@Table(name="erp_role") //数据库中表名,列名需要与实体类中列名对应,如不对应需要使用@Column注解配置
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //主键生成策略
private Integer id;
private String name;
private String description;
//省略get和set方法
}
(2)测试类
public class Test {
@org.junit.Test
public void testAddRole(){
Role role = new Role();
role.setName("角色2");
role.setDescription("角色2");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa"); //读取Resource目录下META-INF文件夹中的persistence.xml文件中的配置信息
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(role);
tx.commit();
}
@org.junit.Test
public void testGetRole(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
//Query query = em.createNativeQuery("select * from erp_role",Role.class);
TypedQuery<Role> query = em.createQuery("from Role",Role.class);
List<Role> roles = query.getResultList();
roles.forEach(role -> System.out.println(role.getName()));
tx.commit();
}
@org.junit.Test
public void testUpdateRole(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Role role = em.find(Role.class,5);
role.setName("角色3");
em.merge(role);
tx.commit();
}
@org.junit.Test
public void testRemoveRole(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Role role = em.find(Role.class,5);
em.remove(role);
tx.commit();
}
}
二.带外键表的级联增加和删除
(1)实体类(订单和订单明细)
@Entity
@Table(name = "erp_order")
public class Order {
@Id
@GenericGenerator(strategy = "uuid",name = "idgen") //uuid主键生成策略
@GeneratedValue(generator = "idgen")
private String id;
private String num;
@OneToMany(mappedBy = "order",cascade = {CascadeType.PERSIST,CascadeType.REMOVE})
private Set<OrderInfo> orderInfos = new HashSet<OrderInfo>();
//省略set和set方法
}
@Entity
@Table(name = "erp_orderinfo")
public class OrderInfo {
@Id
@GenericGenerator(strategy = "uuid",name = "idgen")
@GeneratedValue(generator = "idgen")
private String id;
private String name;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "orderid")
private Order order;
//省略set和set方法
}
(2)测试类
public class OrderTest {
private static EntityManagerFactory emf;
private EntityManager em;
private EntityTransaction tx;
@BeforeClass
public static void init() {
emf = Persistence.createEntityManagerFactory("jpa");
}
@AfterClass
public static void destory() {
emf = null;
}
@Before
public void setUp() {
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
}
@After
public void tearDown() {
tx.commit();
em.close();
}
@Test
public void testAddOrder() {
Order order = new Order();
order.setNum("1002");
OrderInfo orderInfo1 = new OrderInfo();
orderInfo1.setName("商品1");
OrderInfo orderInfo2 = new OrderInfo();
orderInfo2.setName("商品2");
orderInfo1.setOrder(order);
orderInfo2.setOrder(order);
order.getOrderInfos().add(orderInfo1);
order.getOrderInfos().add(orderInfo2);
em.persist(order);
System.out.println(em.contains(order));
}
@Test
public void testDeleteOrder() {
Order order = em.find(Order.class, "2c8d9596614b411f01614b4122120000");
em.remove(order);
}
}