zoukankan      html  css  js  c++  java
  • 转::持久化实体persist()--往数据表中插入数据

    对于你的DAO层应用来说,主要的工作就是将EntityManager管理的实体持久化到数据库中保存起来,即将内存中的实体对象写入到数据表中,在表中反应的是新增了一行记录。

    持久化的方法是:

    1. em.persist(obj); 

    例如,我们将一个学生实体保存到数据库:

    1. try {  
    2.     Student student = new Student();  
    3.     student.setName("刘中兵");  
    4.     student.setSex(true);  
    5.     student.setAge((short)25);  
    6.     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
    7.     student.setBirthday(format.parse("1981-05-04"));  
    8.     student.setTelephone("12345678");  
    9.     student.setAddress("北京");  
    10.     em.persist(student);  
    11. catch (Exception e) {  
    12.     e.printStackTrace();  

    以上的代码将会在数据表student中插入一行记录,类似于执行了以下SQL语句:

    1. insert into student(name, sex, age, birthday, telephone, address)  
    2.          values('刘中兵'125'1981-05-04''12345678''北京'); 

    如果传递进persist()方法的参数不是实体Bean,则会引发IllegalArgumentException异常。

    http://book.51cto.com/art/200909/149968.htm

  • 相关阅读:
    Aizu 0525 Osenbei 搜索 A
    PAT 1088 三人行 模拟,坑 C
    POJ1862 Stripies 贪心 B
    ZOJ 4109 Welcome Party 并查集+优先队列+bfs
    POJ 3685 Matrix
    POJ 3579 Median 二分加判断
    Educational Codeforces Round 63 D. Beautiful Array
    Codeforces Round #553 (Div. 2) C
    HDU 5289
    Codeforces 552 E. Two Teams
  • 原文地址:https://www.cnblogs.com/myitmylife/p/3594120.html
Copyright © 2011-2022 走看看