我正在使用hibernate条件从数据库获取数据,现在我想将该数据插入另一个表中。
这是我的DAO课程
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Post> getPostList() throws Exception {
session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Post.class).setResultTransformer(Transformers.aliasToBean(Result.class));
ProjectionList projList = Projections.projectionList();
projList.add(Projections.sum("val"), "topValue");
projList.add(Projections.groupProperty("userId"), "uid");
cr.setProjection(projList);
List postList = cr.list();
tx = session.getTransaction();
session.beginTransaction();
tx.commit();
return postList;
}
这是我的Post类
@Entity
@Table(name="post")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Post implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name="id")
private long id;
@Column(name="uid")
private long userId;
@Column(name="value")
private long val;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public long getVal() {
return val;
}
public void setVal(long val) {
this.val = val;
}
}
这是我的控制器
@RequestMapping(value = "/posts", method = RequestMethod.GET)
public @ResponseBody
List<Post> getEmployee() {
List<Post> postList = null;
try {
postList = profileService.getPostList();
} catch (Exception e) {
e.printStackTrace();
}
return postList;
}
请建议我做正确的方法我是hibernate的新手
答案 0 :(得分:0)
您是否曾尝试通过@PersistenceContext将交易留给弹簧容器?
public class SampleServiceImpl implements SampleService {
@PersistenceContext(type = PersistenceContextType.TRANSACTION)
protected EntityManager entityManager;
public static final Logger LOG = Logger.getLogger(SampleServiceImpl.class);
@Override
@Transactional
public void retrieveANdSAve(){
Session session =(Session)entityManager.getDelegate() ;
Criteria cr = session.createCriteria(ProductRequest.class);
List <Product> results = cr.list();
for(Product pro: results){
entityManager.merge(whatever object);
}
}
**Note: u must have properly done your orm/jdbc configurations right i your xml config or Annotation config
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="${hibernate.database}" />
<property name="showSql" value="${hibernate.showSql}" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.hbm2ddl.auto" value="update/create" />
<entry key="hibernate.format_sql" value="true" />
<entry key="hibernate.show_sql" value="false" />
<entry key="hibernate.dialect" value="" />
<!-- Connection pooling -->
<entry key="hibernate.c3p0.min_size" value="5" />
<entry key="hibernate.c3p0.max_size" value="20" />
<entry key="hibernate.c3p0.timeout" value="1800" />
<entry key="hibernate.c3p0.max_statements" value="50" />
<entry key="hibernate.jdbc.batch_size" value="100" />
<!-- Auditing -->
<!-- entry key="hibernate.ejb.interceptor" value="org.common.hibernate.AuditInterceptor" /> -->
<!-- Caching -->
<!--
<entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" /> <entry key="hibernate.cache.use_query_cache" value="true" />
-->
<!-- Extra debugging -->
<entry key="hibernate.generate_statistics value" value="true" />
<entry key="hibernate.cache.use_structured_entries" value="true" />
<!-- Lucene -->
<entry key="hibernate.search.default.indexBase" value="${application.dataDir}/lucene" />
<entry key="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider" />
<!-- JSR 303 Bean Validation -->
<entry key="javax.persistence.validation.mode" value="${validation.mode}" />
<entry key="javax.persistence.validation.group.pre-persist" value="${validation.group.pre-persist}" />
<entry key="javax.persistence.validation.group.pre-update" value="${validation.group.pre-update}" />
<entry key="javax.persistence.validation.group.pre-remove" value="${validation.group.pre-remove}" />
</map>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value=" " />
<property name="url" value=" " />
<property name="username" value="" />
<property name="password" value="" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />