1. JPA环境集成中需要在META-INF下配置persistence.xml,涉及相关Jar包
ojdbc14.jar
ejb3-persistence.jar
hibernate-entitymanager.jar
hibernate3.jar
dom4j-1.6.1.jar
hibernate-annotations.jar
commons-logging.jar
hibernate-commons-annotations.jar
javassist.jar
commons-collections-2.1.1.jar
cglib-2.1.3.jar
asm.jar
2. Log4J相关日志配置,打印相关DEBUG错误信息,可以打印到前台和后台日志
### set log levels ###
log4j.rootLogger=debug,stdout,D,E
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
log4j.appender.D=org.apache.log4j.FileAppender
log4j.appender.D.File=logs/log.log
log4j.appender.D.Append=true
log4j.appender.D.Threshold=DEBUG
log4j.appender.D.layout=org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
log4j.appender.E=org.apache.log4j.FileAppender
log4j.appender.E.File=logs/error.log
log4j.appender.E.Append=true
log4j.appender.E.Threshold=ERROR
log4j.appender.E.layout=org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
3. Entity 生成主键策略如下
@Entity
@SequenceGenerator(name="seq_producttype",sequenceName="seq_producttype")
public class ProductType implements Serializable{
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seq_producttype")
/*类别ID*/
private Integer typeid;
4. 搭建Spring还需要相关Jar包
commons-logging.jar
aspectjrt.jar
aspectjweaver.jar
spring.jar
5. 由于Spring的beans.xml中已经配置了DataSource信息,在beans.xml中加载persistence.xml时相关连接配置需要删除
beans.xml
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
</bean>
persistence.xml
<persistence-unit name="oracle">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.jdbc.fetch_size" value ="18"/>
<property name="hibernate.jdbc.batch_size" value ="10"/>
<property name="hibernate.show_sql" value ="true"/>
<property name="hibernate.format_sql" value ="true"/>
</properties>
</persistence-unit>
6. 父类,级联刷新,级联删除,mappedBy
@OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="parent")
a) 只有OneToOne,OneToMany,ManyToMany上才有mappedBy属性,ManyToOne不存在该属性;
b) mappedBy标签一定是定义在the owned side(被拥有方的),他指向the owning side(拥有方);
c) mappedBy跟JoinColumn/JoinTable总是处于互斥的一方,可以理解为正是由于拥有方的关联被拥有方的字段存在,拥有方才拥有了被拥有方。
mappedBy这方定义的JoinColumn/JoinTable总是失效的,不会建立对应的字段或者表。
子类
@ManyToOne(cascade=CascadeType.REFRESH)
@JoinColumn(name="parentid")表示数据库表中需要增加parentid的字段
7. Service层如果不写Transactional,插入数据会回滚掉。
8. em.remove(em.getReference(Entity.class, EntityId))
em.getReference不会真正去做数据库的select,而是假装把Entity的实例给你了。等你真正去做相应操作的时候由JPA Implementation决定如何去读。
操作 SQL
em.remove(em.getReference(Entity.class, EntityId))
1) delete from Entity where id = EntityId.
em.remove(em.find(Entity.class, EntityId))
1)select * from Entity where id = EntityId
2) delete from Entity where id = EntityId.
9. HQL中getScrollData函数的例子,设计的比较好,是不是HQL的范式?
@SuppressWarnings("unchecked") @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED) public <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstIndex, int maxResult, String wherehql, Object[] params, LinkedHashMap<String, String> orderBy) { QueryResult<T> qr = new QueryResult<T>(); String entityname = getEntityName(entityClass); Query query = em.createQuery("select o from " + entityname + " o "+ (wherehql == null ? "" : ("where "+ wherehql)) + buildOrderBy(orderBy)); System.out.println(query.toString()); setParms(query,params); if(firstIndex!=-1 && maxResult!=-1){ query.setFirstResult(firstIndex).setMaxResults(maxResult); } System.out.println(query); qr.setResultlist(query.getResultList()); query = em.createQuery("select count(o) from " + entityname + " o "+ (wherehql == null ? "" : ("where "+ wherehql))); setParms(query,params); qr.setTotalrecord((Long) query.getSingleResult()); return qr; }
遗留问题
1. xml中xmlns,xsi,schemalocation这些命名空间分别是啥意思?
<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">