我正在尝试使用CDI-weld,JSF 2-mojarra,RichFaces 4,JPA-hibernate,Bean-Validation-hibernate,maven 2,Tomcat 7和Eclipse Juno来设置项目。
我觉得感受最新东西的协同作用会很有趣(除了maven,懒得进入maven 3)
除了拼图中的最后一块,添加JPA之外,我的一切都像魅力一样。有很多关于如何设置的信息,经过一段时间的尝试后,我感到非常困惑。我基本上把它缩小到了这个范围:
(来自这篇文章:what dependencies my project should have if I'm using JPA in Hibernate?)
你需要: slf4j-jdk14和hibernate-entitymanager作为工件,就是这样吗?我尝试了一些不同的设置,但我没有任何工作。它只是不会生成表格。(现在我正在尝试使用这些工件,我想我以后会担心版本)
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.1-Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<!-- version 1.5.8 is the latest version that will work with the slf4j-api
that's currently bundled with hibernate-parent -->
<version>1.5.8</version>
</dependency>
persistance.xml 位于:src - main - wewbapp - WEB-INF - classes - META-INF - persistance.xml。
尝试了一堆没有运气的地方。
内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://svn.apache.org/repos/asf/geronimo/components/geronimo-schema-javaee_6/trunk/src/main/xsd/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="pu">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.test.User</class>
<properties>
<!-- Auto detect annotation model classes -->
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="1111" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/woot" />
</properties>
</persistence-unit>
</persistence>
借入测试类:
/**
* User account that is required for logging in and posting bookmarks.
*
* @author Andy Gibson
*
*/
@Entity
@Table(name = "USERS")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(length = 24)
private String username;
@Column(length = 24,name="userPassword")
private String password;
@Column(length = 24)
private String firstName;
@Column(length = 24)
private String lastName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Tomcat日志:
apr 21, 2012 10:58:54 EM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: E:\Programming\Setup\Glassfish\jdk7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;E:\Programming\Setup\Glassfish\jdk7\jre\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\Microsoft Windows Performance Toolkit\;E:\Programming\Setup\Glassfish\jdk7\bin;C:\Program Files (x86)\Groovy\Groovy-1.8.0\bin;C:\Program Files (x86)\Calibre2\;E:\Programming\Setup\apache-maven-2.2.1\bin;E:\Program\SVN\bin;E:\Programming\grails-2.0.0\\bin;.
apr 21, 2012 10:58:54 EM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:woot' did not find a matching property.
apr 21, 2012 10:58:54 EM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
apr 21, 2012 10:58:54 EM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
apr 21, 2012 10:58:54 EM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 900 ms
apr 21, 2012 10:58:54 EM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
apr 21, 2012 10:58:54 EM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.22
apr 21, 2012 10:58:55 EM org.apache.catalina.core.StandardContext addApplicationListener
INFO: The listener "com.sun.faces.config.ConfigureListener" is already configured for this context. The duplicate definition has been ignored.
apr 21, 2012 10:58:55 EM org.jboss.weld.bootstrap.WeldBootstrap <clinit>
INFO: WELD-000900 1.1.5 (Final)
apr 21, 2012 10:58:55 EM org.jboss.weld.bootstrap.WeldBootstrap startContainer
INFO: WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
apr 21, 2012 10:58:56 EM org.jboss.weld.environment.tomcat7.Tomcat7Container initialize
INFO: Tomcat 7 detected, CDI injection will be available in Servlets and Filters. Injection into Listeners is not supported
apr 21, 2012 10:58:56 EM org.jboss.interceptor.util.InterceptionTypeRegistry <clinit>
WARNING: Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
apr 21, 2012 10:58:56 EM org.jboss.interceptor.util.InterceptionTypeRegistry <clinit>
WARNING: Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled
apr 21, 2012 10:58:56 EM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Initializing Mojarra 2.1.7 (SNAPSHOT 20120206) for context '/rdwebref'
apr 21, 2012 10:58:58 EM com.sun.faces.spi.InjectionProviderFactory createInstance
INFO: JSF1048: PostConstruct/PreDestroy annotations present. ManagedBeans methods marked with these annotations will have said annotations processed.
apr 21, 2012 10:58:59 EM org.hibernate.validator.util.Version <clinit>
INFO: Hibernate Validator 4.0.2.GA
apr 21, 2012 10:58:59 EM org.hibernate.validator.engine.resolver.DefaultTraversableResolver detectJPA
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
apr 21, 2012 10:59:00 EM org.hibernate.validator.engine.resolver.DefaultTraversableResolver detectJPA
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
apr 21, 2012 10:59:00 EM org.richfaces.cache.CacheManager getCacheFactory
INFO: Selected fallback cache factory
apr 21, 2012 10:59:00 EM org.richfaces.cache.lru.LRUMapCacheFactory createCache
INFO: Creating LRUMap cache instance using parameters: {javax.servlet.jsp.jstl.fmt.localizationContext=resources.application, javax.faces.STATE_SAVING_METHOD=client, javax.faces.DEFAULT_SUFFIX=.xhtml}
apr 21, 2012 10:59:00 EM org.richfaces.cache.lru.LRUMapCacheFactory createCache
INFO: Creating LRUMap cache instance of 512 items capacity
apr 21, 2012 10:59:00 EM org.richfaces.application.InitializationListener onStart
INFO: RichFaces Core Implementation by JBoss by Red Hat, version v.4.2.0.Final
apr 21, 2012 10:59:00 EM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
apr 21, 2012 10:59:00 EM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
apr 21, 2012 10:59:00 EM org.apache.catalina.startup.Catalina start
INFO: Server startup in 6130 ms
我确实创建了名为“woot”的数据库,密码和用户名是正确的。我之前已经使用了mysql与Grails 2一起工作。
希望有人可以帮助我!欢呼声
答案 0 :(得分:0)
基本上,有两种使用JPA的方法:
在此模式下使用有两个好处:
一个。如果使用会话Bean,则可以通过EJB框架注入实体管理器,无需焊接;
湾事务控制由EJB Containter(JTA)自动完成。
如果您不处理EJB容器,这是使用JPA的最常用方法。 为了避免样板代码并使事情变得更容易,建议注射管理器。 Spring,Guice,Pico是很好的选择,但是Weld也很好,并且因为它实现了Java的注射标准CDI而略胜一筹。
我最好的办法是寻找有关如何使用Weld在Tomcat应用程序中注入EntityManager的更多信息。我发现这个链接有一个很长的讨论:
https://community.jboss.org/thread/179355
简而言之:使用Seam 3。
祝你好运