Myeclipse6.0实现JPA
2008-04-01 01:15
302 查看
1.首先先创建一个JAVA项目,之后对其添入JPA(项目-->右键-->MyEclipse->Add JPA 功能) ;
2.一般JPA对Toplink的支持比对hibernate的支持好点,所以我们直接默认就好,-->next;
3.选择JPA配置的数据源,(Driver 、catalog/Schema..) -->Finish ;
4.选中其中的一个表,右键-->创建JPA 反向工程 ;(我创建了一个表MyUser里面有字段:id pk,name varchar(20),password varchar(20) 。)
5.先设置放置的路径,我放在一个jpaDaoTest包中,产生一个实体Bean并更改persistence.xml产生一个实体。persistence.xml:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<persistence-unit name="jpaTestPU"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
transaction-type="RESOURCE_LOCAL">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<provider>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
oracle.toplink.essentials.PersistenceProvider
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</provider>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class>jpaDao.MyUser</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class>jpaDaoTest.EisArea</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<properties>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="toplink.jdbc.driver"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="toplink.jdbc.url"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
value="jdbc:microsoft:sqlserver://210.51.168.147:1433;DatabaseName=www_souvi_com" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="toplink.jdbc.user" value="web1621510" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="toplink.jdbc.password" value="fhq2020" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</properties>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</persistence-unit>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</persistence>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
之后在勾选上 Java Data Access Generation创建接口和其实现方法。类型为Basic DAO默认即可。
6.Finish ;
MyUser.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package jpaDao;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.Column;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.Entity;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.Id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.Table;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.GenerationType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.GeneratedValue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* MyUser generated by MyEclipse Persistence Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@Entity
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
@Table(catalog = "web1621510",schema = "www_souvi_com",name = "MyUser",uniqueConstraints = ...{})
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class MyUser implements java.io.Serializable ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Fields
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Integer id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String password;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Constructors
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//** default constructor */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public MyUser() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//** minimal constructor */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public MyUser(Integer id) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//** full constructor */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public MyUser(Integer id, String name, String password) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.password = password;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Property accessors
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Column(name = "id", unique = true, nullable = false, insertable = true, updatable = true)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@GeneratedValue(strategy=GenerationType.IDENTITY)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Integer getId() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return this.id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setId(Integer id) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Column(name = "name", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getName() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return this.name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setName(String name) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Column(name = "password", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getPassword() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return this.password;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setPassword(String password) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.password = password;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
IMyUserDAO.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package jpaDao;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Interface for MyUserDAO.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author MyEclipse Persistence Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public interface IMyUserDAO ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void save(MyUser transientInstance);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void delete(MyUser persistentInstance);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public MyUser update(MyUser detachedInstance);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public MyUser findById(Integer id);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public List<MyUser> findByProperty(String propertyName, Object value);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public List<MyUser> findByName(Object name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public List<MyUser> findByPassword(Object password);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public List<MyUser> findAll();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
MyUserDAO.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package jpaDao;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.logging.Level;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.EntityManager;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Data access object (DAO) for domain model class MyUser.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @see jpaDao.MyUser
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author MyEclipse Persistence Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class MyUserDAO implements IMyUserDAO ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// property constants
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static final String NAME = "name";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static final String PASSWORD = "password";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private EntityManager getEntityManager() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return EntityManagerHelper.getEntityManager();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void save(MyUser transientInstance) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("saving MyUser instance", Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
getEntityManager().persist(transientInstance);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("save successful", Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (RuntimeException re) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("save failed", Level.SEVERE, re);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw re;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void delete(MyUser persistentInstance) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("deleting MyUser instance", Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
getEntityManager().remove(persistentInstance);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("delete successful", Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (RuntimeException re) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("delete failed", Level.SEVERE, re);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw re;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public MyUser update(MyUser detachedInstance) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("updating MyUser instance", Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyUser result = getEntityManager().merge(detachedInstance);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("update successful", Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (RuntimeException re) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("update failed", Level.SEVERE, re);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw re;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public MyUser findById(Integer id) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("finding MyUser instance with id: " + id,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyUser instance = getEntityManager().find(MyUser.class, id);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return instance;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (RuntimeException re) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("find failed", Level.SEVERE, re);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw re;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@SuppressWarnings("unchecked")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public List<MyUser> findByProperty(String propertyName, Object value) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("finding MyUser instance with property: "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
+ propertyName + ", value: " + value, Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String queryString = "select model from MyUser model where model."
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
+ propertyName + "= :propertyValue";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return getEntityManager().createQuery(queryString).setParameter(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"propertyValue", value).getResultList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (RuntimeException re) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("find by property name failed",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Level.SEVERE, re);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw re;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public List<MyUser> findByName(Object name) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return findByProperty(NAME, name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public List<MyUser> findByPassword(Object password) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return findByProperty(PASSWORD, password);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@SuppressWarnings("unchecked")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public List<MyUser> findAll() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("finding all MyUser instances", Level.INFO,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String queryString = "select model from MyUser model";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return getEntityManager().createQuery(queryString).getResultList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (RuntimeException re) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("find all failed", Level.SEVERE, re);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw re;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
现在我们写一个测试类吧,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import jpaDao.* ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Test ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param args
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 开始事务
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.beginTransaction();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 创建DAO
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyUserDAO dao = new MyUserDAO();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 创建对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyUser user = new MyUser() ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
user.setName("chenhao") ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
user.setPassword("password");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//保存对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dao.save(user) ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//事务提交
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.commit() ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("++++++++++++++++++++++++");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 得到所有的数据并遍利
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<MyUser> result = dao.findAll() ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(MyUser o : result)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("id = " + o.getId()) ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("name = " + o.getName()) ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("password = " + o.getPassword()) ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
console:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[TopLink Info]: 2008.04.01 12:26:29.484--ServerSession(26680060)--TopLink, version: Oracle TopLink Essentials - 2.0 (Build b40-rc (03/21/2007))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[TopLink Info]: 2008.04.01 12:27:08.437--ServerSession(26680060)--file:/D:/workspace/jpaTest/bin/-jpaTestPU login successful
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2008-4-1 0:27:08 jpaDao.EntityManagerHelper log
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
信息: saving MyUser instance
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2008-4-1 0:27:08 jpaDao.EntityManagerHelper log
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
信息: save successful
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
++++++++++++++++++++++++
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2008-4-1 0:27:08 jpaDao.EntityManagerHelper log
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
信息: finding all MyUser instances
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
id = 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name = chenhao
快试试吧!~
2.一般JPA对Toplink的支持比对hibernate的支持好点,所以我们直接默认就好,-->next;
3.选择JPA配置的数据源,(Driver 、catalog/Schema..) -->Finish ;
4.选中其中的一个表,右键-->创建JPA 反向工程 ;(我创建了一个表MyUser里面有字段:id pk,name varchar(20),password varchar(20) 。)
5.先设置放置的路径,我放在一个jpaDaoTest包中,产生一个实体Bean并更改persistence.xml产生一个实体。persistence.xml:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<persistence-unit name="jpaTestPU"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
transaction-type="RESOURCE_LOCAL">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<provider>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
oracle.toplink.essentials.PersistenceProvider
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</provider>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class>jpaDao.MyUser</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class>jpaDaoTest.EisArea</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<properties>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="toplink.jdbc.driver"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="toplink.jdbc.url"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
value="jdbc:microsoft:sqlserver://210.51.168.147:1433;DatabaseName=www_souvi_com" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="toplink.jdbc.user" value="web1621510" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="toplink.jdbc.password" value="fhq2020" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</properties>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</persistence-unit>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</persistence>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
之后在勾选上 Java Data Access Generation创建接口和其实现方法。类型为Basic DAO默认即可。
6.Finish ;
MyUser.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package jpaDao;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.Column;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.Entity;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.Id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.Table;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.GenerationType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.GeneratedValue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* MyUser generated by MyEclipse Persistence Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@Entity
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
@Table(catalog = "web1621510",schema = "www_souvi_com",name = "MyUser",uniqueConstraints = ...{})
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class MyUser implements java.io.Serializable ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Fields
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Integer id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String password;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Constructors
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//** default constructor */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public MyUser() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//** minimal constructor */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public MyUser(Integer id) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//** full constructor */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public MyUser(Integer id, String name, String password) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.password = password;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Property accessors
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Column(name = "id", unique = true, nullable = false, insertable = true, updatable = true)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@GeneratedValue(strategy=GenerationType.IDENTITY)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Integer getId() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return this.id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setId(Integer id) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Column(name = "name", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getName() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return this.name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setName(String name) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Column(name = "password", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getPassword() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return this.password;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setPassword(String password) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.password = password;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
IMyUserDAO.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package jpaDao;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Interface for MyUserDAO.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author MyEclipse Persistence Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public interface IMyUserDAO ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void save(MyUser transientInstance);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void delete(MyUser persistentInstance);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public MyUser update(MyUser detachedInstance);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public MyUser findById(Integer id);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public List<MyUser> findByProperty(String propertyName, Object value);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public List<MyUser> findByName(Object name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public List<MyUser> findByPassword(Object password);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public List<MyUser> findAll();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
MyUserDAO.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package jpaDao;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.logging.Level;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.persistence.EntityManager;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Data access object (DAO) for domain model class MyUser.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @see jpaDao.MyUser
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author MyEclipse Persistence Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class MyUserDAO implements IMyUserDAO ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// property constants
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static final String NAME = "name";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static final String PASSWORD = "password";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private EntityManager getEntityManager() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return EntityManagerHelper.getEntityManager();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void save(MyUser transientInstance) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("saving MyUser instance", Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
getEntityManager().persist(transientInstance);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("save successful", Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (RuntimeException re) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("save failed", Level.SEVERE, re);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw re;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void delete(MyUser persistentInstance) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("deleting MyUser instance", Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
getEntityManager().remove(persistentInstance);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("delete successful", Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (RuntimeException re) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("delete failed", Level.SEVERE, re);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw re;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public MyUser update(MyUser detachedInstance) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("updating MyUser instance", Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyUser result = getEntityManager().merge(detachedInstance);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("update successful", Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (RuntimeException re) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("update failed", Level.SEVERE, re);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw re;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public MyUser findById(Integer id) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("finding MyUser instance with id: " + id,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyUser instance = getEntityManager().find(MyUser.class, id);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return instance;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (RuntimeException re) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("find failed", Level.SEVERE, re);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw re;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@SuppressWarnings("unchecked")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public List<MyUser> findByProperty(String propertyName, Object value) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("finding MyUser instance with property: "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
+ propertyName + ", value: " + value, Level.INFO, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String queryString = "select model from MyUser model where model."
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
+ propertyName + "= :propertyValue";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return getEntityManager().createQuery(queryString).setParameter(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"propertyValue", value).getResultList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (RuntimeException re) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("find by property name failed",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Level.SEVERE, re);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw re;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public List<MyUser> findByName(Object name) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return findByProperty(NAME, name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public List<MyUser> findByPassword(Object password) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return findByProperty(PASSWORD, password);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@SuppressWarnings("unchecked")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public List<MyUser> findAll() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("finding all MyUser instances", Level.INFO,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String queryString = "select model from MyUser model";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return getEntityManager().createQuery(queryString).getResultList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (RuntimeException re) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.log("find all failed", Level.SEVERE, re);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw re;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
现在我们写一个测试类吧,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import jpaDao.* ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Test ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param args
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 开始事务
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.beginTransaction();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 创建DAO
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyUserDAO dao = new MyUserDAO();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 创建对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyUser user = new MyUser() ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
user.setName("chenhao") ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
user.setPassword("password");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//保存对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dao.save(user) ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//事务提交
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EntityManagerHelper.commit() ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("++++++++++++++++++++++++");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 得到所有的数据并遍利
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<MyUser> result = dao.findAll() ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(MyUser o : result)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("id = " + o.getId()) ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("name = " + o.getName()) ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("password = " + o.getPassword()) ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
console:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[TopLink Info]: 2008.04.01 12:26:29.484--ServerSession(26680060)--TopLink, version: Oracle TopLink Essentials - 2.0 (Build b40-rc (03/21/2007))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[TopLink Info]: 2008.04.01 12:27:08.437--ServerSession(26680060)--file:/D:/workspace/jpaTest/bin/-jpaTestPU login successful
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2008-4-1 0:27:08 jpaDao.EntityManagerHelper log
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
信息: saving MyUser instance
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2008-4-1 0:27:08 jpaDao.EntityManagerHelper log
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
信息: save successful
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
++++++++++++++++++++++++
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2008-4-1 0:27:08 jpaDao.EntityManagerHelper log
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
信息: finding all MyUser instances
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
id = 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name = chenhao
快试试吧!~
相关文章推荐
- Myeclipse6.0实现JPA 创建
- Myeclipse实现JPA 创建
- Myeclipse实现JPA 创建
- MyEclipse 6.0 发布- 整合 Spring/JPA Integration, Java 6
- MyEclipse 6.0 发布- 整合 Spring/JPA Integration, Java 6
- MyEclipse 6.0 发布- 整合 Spring/JPA Integration, Java 6
- 用VC 6.0实现串行通信的三种方法
- myeclipse 6.0 注册码 (有效期至2012年)
- Centos6.0之mysql+freeradiu实现帐号统一认证
- 基于VC 6.0使用C语言实现俄罗斯方块
- hibernate(jpa)根据实体动态生成查询条件,并实现分页问题的解决方案
- 【Hades】ades是一个开源库,基于JPA和Spring构建,通过减少开发工作量显著的改进了数据访问层的实现
- iOS6.0 消息推送的原理与实现
- Myeclipse与tomcatplugin实现应用布署
- JPA入门例子(采用JPA的hibernate实现版本)
- 纯手工系列--零配置实现Spring MVC+Spring Data JPA
- MYECLIPSE6.0中配置SSH
- eclipse、MyEclipse实现批量修改文件编码
- 在Spring Boot中使用Spring-data-jpa实现分页查询(转)
- myeclipse6.0 安装svn插件