我来自Spring阵营,我不想使用Spring,并且正在迁移到JavaEE6,但我测试DAO + JPA有问题,这是我的简化示例:
public interface PersonDao
{
public Person get(long id);
}
Run Code Online (Sandbox Code Playgroud)
这是一个非常基本的DAO,因为我来自Spring,我相信DAO仍然有它的价值,所以我决定添加一个DAO层.
public class PersonDaoImpl implements PersonDao , Serializable
{
@PersistenceContext(unitName = "test", type = PersistenceContextType.EXTENDED)
EntityManager entityManager ;
public PersonDaoImpl()
{
}
@Override
public Person get(long id)
{
return entityManager .find(Person.class , id);
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个JPA实现的DAO,我希望EE容器或测试容器能够注入EntityManager(就像Spring一样).
public class PersonDaoImplTest extends TestCase
{
@Inject
protected PersonDao personDao;
@Override
protected void setUp() throws Exception
{
//personDao = new PersonDaoImpl();
}
public void testGet()
{
System.out.println("personDao = " + personDao); // …Run Code Online (Sandbox Code Playgroud) 是否建议将小循环(如果可能)从长度 - 1减少到零,而不是计算长度 - 1?
1.)倒计时
for (int i = a.length - 1; i >= 0; i--) {
if (a[i] == key) return i;
}
Run Code Online (Sandbox Code Playgroud)
2.)数数
for (int i = 0; i < a.length; i++) {
if (a[i] == key) return i;
}
Run Code Online (Sandbox Code Playgroud)
第一个比第二个快一点(因为比较零更快)但在我看来更容易出错.此外,第一个可能无法通过JVM的未来改进进行优化.有什么想法吗?
我想从 Maven 中创建的项目生成一个 .jar 应用程序。
我在 Eclipse 中工作,我做了: run as/Maven assembly: assembly
这是错误消息:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-4:assembly (default-cli) on project FeelIndexer: Error reading assemblies: No assembly descriptors found.
Run Code Online (Sandbox Code Playgroud)
这是我的assamble.xml
<assembly>
<id>exe</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory></outputDirectory>
<outputFileNameMapping></outputFileNameMapping>
<unpack>true</unpack>
<scope>runtime</scope>
<includes>
<include>commons-lang:commons-lang</include>
<include>commons-cli:commons-cli</include>
</includes>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory></outputDirectory>
</fileSet>
</fileSets>
</assembly>
Run Code Online (Sandbox Code Playgroud)
我想我必须包含其他内容来添加 muy 项目的依赖项,但我不知道该怎么做!建议??
更新:在我的 assembly.xml 下方
<assembly>
<id>exe</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory></outputDirectory>
<outputFileNameMapping></outputFileNameMapping>
<unpack>true</unpack>
<scope>runtime</scope>
<includes>
<include>commons-lang:commons-lang</include>
<include>commons-cli:commons-cli</include>
</includes> …Run Code Online (Sandbox Code Playgroud) 我有两个A,B相互关联的表(简化):
A:
+-------+---------+
| id | type |
+-------+---------+
| 1 | apple |
| 2 | orange |
| 3 | banana |
+-------+---------+
B:
+-------+---------+-----------+
| id | a_id | rank |
+-------+---------+-----------+
| 1 | 1 | 9.9 |
| 2 | 1 | 7.7 |
| 3 | 2 | 3.3 |
| 4 | 2 | 8.8 |
| 5 | 2 | 1.1 |
| 6 | 3 | 3.3 |
| …Run Code Online (Sandbox Code Playgroud) 我在Java EE 6应用程序服务器(GlassFish v3)中使用带有Facelets的JSF 2.0.我在web.xml中为异常配置了一个错误页面:
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error-all.xhtml</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
这是/error-all.xhtml测试页面:
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
template="/resources/templates/decorator.xhtml">
<ui:define name="title">Title</ui:define>
<ui:define name="body">
<h1>Body</h1>
</ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
我实现了一个托管bean,当我点击我的一个commandLinks时,它会故意抛出RuntimeException.当发生这种情况时,会显示/error-all.xhtml页面的内容,但它不会通过Facelets获取进程,因此不会应用template ="/ resources/templates/decorator.xhtml".
使用谷歌浏览器,我只看到"标题"和"正文",结果没有布局.如果我要求Chrome检查这些元素,我会得到完整的源代码,其中包括ui:composition和ui:define标记,Chrome显然无法理解.这证实了我的理论,即Facelets页面没有被处理.
所以,我的问题是,如何解决这个问题?如何处理错误页面并返回HTML代码,该代码是模板与错误页面内容组合的结果?
我正在开发一个使用Spring和Hibernate的Web应用程序,我非常痴迷于使应用程序线程安全,并且能够支持基于我的老板建议的重负载,我最终编写自己的session并且session container实现了session per request pattern.另外,我有很多DAOs并且我不愿意为我save method所有的DAOs复制粘贴这个Hibernate写相同的内容GenericDAO(我不能说它是同样的事情,因为当时hibernate不是由jboss拥有)并且做管道工作,并且在压力下,所有变得很快复杂并且在生产中,StaleObjectException和重复数据正确,我我觉得是时候回顾一下我所做的事情,简化它并使其更适合大数据处理.你应该知道的一件事是,一个请求涉及许多DAO.
有石英运行数据库中的一些更新.
尽管我想要更好地调整所有内容,但我没有时间进行必要的研究,而Hibernate则是一种巨大的(学习).
所以就是这样,我想借用你的经验,并提出几个问题,知道要采取什么方向.
问题1:Hibernate生成的uuid足够安全,可以用于线程环境并避免使用StaleObjectException吗?
问题2什么是在threadSafe场景中使用hibernate getCurrentSession的最佳策略(我已经阅读了关于threadlocal的东西,但没有得到太多的理解,所以没有这样做)
问题3:HIbernateTemplate会采用最简单的解决方案吗?
问题4:如果要实现连接池并调整生产服务器的需求,您会选择什么?
请毫不犹豫地将我指向博客或在线资源,我需要的只是一种适用于我的场景的方法.如果你这样做,你的方法.
感谢您阅读本文,欢迎大家的想法......
我的hibernate实体如下:
@Entity
@Table(name = "EditLocks")
public class EditLock extends AuditableEntity {
/** The document that is checked out. */
@OneToOne
@JoinColumn(name = "documentId", nullable = false)
private Document document;
Run Code Online (Sandbox Code Playgroud)
文档然后看起来像这样:
public class Document extends AuditableEntity {
/** Type of the document. */
@ManyToOne
@JoinColumn(name = "documentTypeId", nullable = false)
private DocumentType documentType;
Run Code Online (Sandbox Code Playgroud)
基本上我想写的查询是:
Select * from EditLocks el, Document docs, DocumentTypes dt where el.documentId = docs.id and docs.documentTypeId = dt.id and dt.id = 'xysz';
Run Code Online (Sandbox Code Playgroud)
如何使用hibernate条件api执行此操作?
我在eclipse中使用静态文件夹(lib)进行了项目.这个文件夹包含很多库,也是基于maven构建这个项目的过程.我可以自动导入(使用eclipse maven插件)lib文件夹的所有库到maven依赖项,还是我应该手动执行?谢谢.
我希望在点击html页面中的html链接时执行Struts2操作.我的第一个问题是,是否可以在html页面(而不是JSP)中执行Struts2操作?如果是,请查看下面的代码:
home.html的
HREF = "home.action"
在struts.xml
action name ="home"class ="com.struts.action.HomeAction"
Run Code Online (Sandbox Code Playgroud)result name="Success">loginJSP.jsp
*****web.xml*****我做了过滤映射,以便所有内容都转到Struts2
我有几个表,如国家,州城市,有静态数据.用户不要在此数据中输入任何数据.我为国家,州,城市创造了pojo.很少有pojo与静态数据进行映射.我的要求是每当有任何请求进入Hibernate for Country(21)时,它都不会命中数据库而是从缓存中返回数据.这在Hibernate中是否可行.我需要一些指针和你的观点来在我的项目中实现缓存.我正在使用hibernate注释.