JPA是java编程领域的ORM标准。最著名的参考实现是hibernate.
一、主要API
⑴ 实体(@Entity)
⑵ 数据库模式属性(@Table、@SecondaryTable、@SecondaryTables、@Column、@JoinColumn、@JoinColumns、@PrimaryKeyJoinColumn、@PrimaryKeyJoinColumns、@JoinTable、@UniqueConstraint)
⑶ 身份(@Id、@IdClass、@EmbeddedId、@GeneratedValue、@SequenceGenerator、@TableGenerator)
⑷ 直接映射( @Basic、@Enumerated、@Temporal、@Lob、@Transient)
⑸ 关系映射(@OneToOne、@ManyToOne、@OneToMany、@ManyToMany、@MapKey、@OrderBy)
⑹ 组合(@Embeddable、@Embedded、@AttributeOverride、@AttributeOverrides、@AssociationOverride、@AssociationOverrides)
⑺ 继承(@Inheritance、@DiscriminatorColumn、@DiscriminatorValue、@MappedSuperclass、@AssociationOverride、@AssociationOverrides、@AttributeOverride、@AttributeOverrides)
⑻ 锁定(@Version)
⑼ 生命周期回调事件(@PrePersist、@PostPersist、@PreRemove、@PostRemove、@PreUpdate、@PostUpdate、@PostLoad、@EntityListeners、@ExcludeDefaultListeners、@ExcludeSuperclassListeners)
⑽ 实体管理器(@PersistenceUnit、@PersistenceUnits、@PersistenceContext、@PersistenceContexts、@PersistenceProperty)
⑾ 查询(@NamedQuery、@NamedQueries、@NamedNativeQuery、@NamedNativeQueries、@QueryHint、@ColumnResult、@EntityResult、@FieldResult、@SqlResultSetMapping、@SqlResultSetMappings)
注:以上内容引自“http://hi.baidu.com/shirdrn/blog/item/235fd7fbac86c1136d22ebd1.html”
二、依赖环境
1、jdk1.5及以上
2、ejb3-persistence.jar
三、基础配置
1、若ssh风格的配置,不需特别设置。
2、独立的配置需在web工程的META-INF目录中加一个persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="hibernate" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mp?useUnicode=true&characterEncoding=UTF-8"/>
</properties>
</persistence-unit>
</persistence>