Hibernate 3.2支持JPA注解,Spring集成JPA是可选的。
下面是Spring集成JPA的一个例子。
实体类MyUser,使用JPA注解实现到数据库表myUser的映射,如下所示:
package org.shirdrn.entity; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Transient; @Entity public class MyUser { private Long id; private String userName; private String password; private String gender; private Integer age; private Integer birthYear; private String addr; private String email; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="userName") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Column(name="password") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Column(name="gender") public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Column(name="age") public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Column(name="addr") public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } @Column(name="email") public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Transient public Integer getBirthYear() { return new Integer(2008-age); } public void setBirthYear(Integer age) { this.birthYear = new Integer(2008-age); } } |
其中,birthYear不是数据库中的字段,使用JPA的@Transient注解,在映射的时候,会忽略掉该成员。
持久层DAO接口如下:
package org.shirdrn.dao; import java.util.List; import org.shirdrn.entity.MyUser; import org.springframework.transaction.annotation.Transactional; @Transactional public interface MyUserDAO { public void createMyUser(MyUser myUser); public void deleteMyUser(MyUser myUser); public void updateMyUser(MyUser myUser); public List queryMyUser(String queryString); public List queryAll(); } |
这里,使用了JPA注解,声明事务。
DAO实现类,如下所示:
package org.shirdrn.dao.impl; import java.util.List; import org.shirdrn.dao.MyUserDAO; import org.shirdrn.entity.MyUser; import org.springframework.orm.jpa.support.JpaDaoSupport; public class MyUserDAOImpl extends JpaDaoSupport implements MyUserDAO { public void createMyUser(MyUser myUser) { getJpaTemplate().persist(myUser); } public void deleteMyUser(MyUser myUser) { MyUser dbMyUser = getJpaTemplate().find(MyUser.class, myUser.getId()); getJpaTemplate().remove(dbMyUser); } public void updateMyUser(MyUser myUser) { MyUser dbMyUser = getJpaTemplate().find(MyUser.class, myUser.getId()); if(myUser.getUserName() != null){ dbMyUser.setUserName(myUser.getUserName()); } if(myUser.getAddr() != null){ dbMyUser.setAddr(myUser.getAddr()); } getJpaTemplate().merge(dbMyUser); } @SuppressWarnings("unchecked") public List queryMyUser(String queryString) { return (List)getJpaTemplate().find(queryString); } @SuppressWarnings("unchecked") public List queryAll() { return (List)getJpaTemplate().find("from MyUser"); } } |
因为继承了JpaDaoSupport,索引需要获取一个JpaTemplate来实现访问数据库,在Spring的配置文件中要注入一个org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean。
[1] [2] 下一页