我需要使用EJB 3,Hibernate,Spring Data和Oracle进行大量插入.最初,我使用的是Spring Data,代码如下:
talaoAITDAO.save(taloes);
Run Code Online (Sandbox Code Playgroud)
其中talaoAITDAO是Spring Data JpaRepository子类,taloes是TalaoAIT实体的集合.在此实体中,其各自的ID具有以下形式:
@Id
@Column(name = "ID_TALAO_AIT")
@SequenceGenerator(name = "SQ_TALAO_AIT", sequenceName = "SQ_TALAO_AIT", allocationSize = 1000)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SQ_TALAO_AIT")
private Long id;
Run Code Online (Sandbox Code Playgroud)
此实体也没有相关实体进行级联插入.
我的问题是,所有实体都是单独插入的(例如INSERT INTO TABLE(col1, col2) VALUES (val1, val2)).有时,它可能会导致超时,并且所有插入都将被回滚.我想要在批量插入中转换这些单独的插入(例如INSERT INTO TABLE(col1, col2) VALUES (val11, val12), (val21, val22), (val31, val32), ...).
研究替代方案以提高性能,我在hibernate文档中找到了这个页面,超出了 Hibernate批量大小混淆和其他页面.基于它们,我写了这段代码:
Session session = super.getEntityManager().unwrap(Session.class);
int batchSize = 1000;
for (int i = 0; i < taloes.size(); i++) {
TalaoAIT …Run Code Online (Sandbox Code Playgroud) 我正在通过Eclipse在Maven Web项目中工作.在web.xml,我有一个context-param,其值应根据我运行Maven时使用的配置文件而改变.
<context-param>
<param-name>producao</param-name>
<param-value>${ambiente.producao}</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
在项目的pom文件中,我有以下配置:
<project>
<profiles>
<profile>
<id>prod</id>
<properties>
<ambiente.producao>true</ambiente.producao>
</properties>
</profile>
<profile>
<id>desenv</id>
<properties>
<ambiente.producao>false</ambiente.producao>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/webapp/WEB-INF/</directory>
<filtering>true</filtering>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
我resources根据我在互联网上找到的参考资料使用两个标签作为maven-war-plugin插件.但是,它没有按预期工作.在Eclipse中,我运行目标为clean install的maven,以及prod或desenv作为"Profiles" .在我运行Maven后,我观察到,在web.xml该${ambiente.producao}属性中没有被替换.因此,我想知道我做错了什么.我应该只使用Filtering资源还是maven-war-plugin?
谢谢,
Rafael Afonso
我有一个RESTful资源,它调用EJB来进行查询.如果查询没有结果,则EJB抛出EntityNotFoundException.在catch块中,将抛出一个代码为404的javax.xml.ws.http.HTTPException.
@Stateless
@Path("naturezas")
public class NaturezasResource {
@GET
@Path("list/{code}")
@Produces(MediaType.APPLICATION_JSON)
public String listByLista(
@PathParam("code") codeListaNaturezasEnum code) {
try {
List<NaturezaORM> naturezas = this.naturezaSB
.listByListaNaturezas(code);
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(naturezas);
} catch (EntityNotFoundException e) { // No data found
logger.error("there is no Natures with the code " + code);
throw new HTTPException(404);
} catch (Exception e) { // Other exceptions
e.printStackTrace();
throw new HTTPException(500);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用没有结果的代码调用Rest Service时,将EntityNotFoundException打印catch块内的日志消息.但是,我的客户端收到HTTP代码500而不是404.为什么我没有收到404代码?
谢谢,
Rafael Afonso