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

  • 相关阅读:
    ubuntu防火墙设置通过某端口
    pandas入门
    pyplot入门
    numpy教程
    跨域请求 spring boot
    spring boot 启动流程
    代理配置访问
    AOP,拦截器
    spring boot 启动不连接数据库
    Python 3.x 连接数据库(pymysql 方式)
  • 原文地址:https://www.cnblogs.com/myitmylife/p/3594120.html
Copyright © 2011-2022 走看看