なぜこのエラーが消えないのか理解できません。すべてのバージョンの hibernate-entitymanager を使用し、vraptor-jpa 1.0 を追加するなど、いくつかのことを試しましたが、何も機能しません。
エラー:
21:58:20,041 ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to /vraptor/: com.google.inject.CreationException: Guice creation errors:
1) No implementation for javax.persistence.EntityManager was bound.
while locating javax.persistence.EntityManager
for parameter 0 at vraptor.UsuarioDao.<init>(UsuarioDao.java:14)
at br.com.caelum.vraptor.ioc.guice.GuiceComponentRegistry.bindToConstructor(GuiceComponentRegistry.java:151)
1 error
at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435) [guice-3.0.jar:]
at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:154) [guice-3.0.jar:]
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:106) [guice-3.0.jar:]
at com.google.inject.Guice.createInjector(Guice.java:95) [guice-3.0.jar:]
at com.google.inject.Guice.createInjector(Guice.java:83) [guice-3.0.jar:]
at br.com.caelum.vraptor.ioc.guice.GuiceProvider.start(GuiceProvider.java:97) [vraptor-3.5.3.jar:]
at br.com.caelum.vraptor.VRaptor.init(VRaptor.java:119) [vraptor-3.5.3.jar:]
at br.com.caelum.vraptor.VRaptor.init(VRaptor.java:113) [vraptor-3.5.3.jar:]
at io.undertow.servlet.core.ManagedFilter.getFilter(ManagedFilter.java:69) [undertow-servlet-1.0.0.Beta30.jar:1.0.0.Beta30]
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:56) [undertow-servlet-1.0.0.Beta30.jar:1.0.0.Beta30]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.0.0.Beta30.jar:1.0.0.Beta30]
...
IndexController.java:
package vraptor;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
@Resource
public class IndexController {
private final Result result;
private final UsuarioDao userDao;
public IndexController(Result result, UsuarioDao userDao) {
this.result = result;
this.userDao= userDao;
}
@Path("/")
public void index() {}
@Post @Path("/cadastro")
public void cadastro(Usuario usuario){
//System.out.println(usuario);
this.userDao.InsertUser(usuario);
this.result.forwardTo(IndexController.class).index();
}
}
ウスアリオDAO
package vraptor;
import javax.persistence.EntityManager;
import br.com.caelum.vraptor.ioc.Component;
@Component
public class UsuarioDao {
//private EntityManager em = new EntityManagerCreator().getInstance();
private EntityManager em;
public UsuarioDao(EntityManager em){
this.em = em;
}
public void InsertUser(Usuario usuario) {
try{
this.em.persist(usuario);
}catch(Exception e){
e.printStackTrace();
}
}
}
Usuario.java
package vraptor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Usuario {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private long idUsuario;
private String nome;
private String sobreNome;
public long getIdUsuario() {
return idUsuario;
}
public void setIdUsuario(long idUsuario) {
this.idUsuario = idUsuario;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSobreNome() {
return sobreNome;
}
public void setSobreNome(String sobreNome) {
this.sobreNome = sobreNome;
}
}
永続性:
<persistence version="1.0" 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">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>vraptor.Usuario</class>
<properties>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.password" value="12345"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost/teste"/>
<property name="hibernate.connection.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show.sql" value="false"/>
<property name="hibernate.format.sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
そして pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>modelo0</groupId>
<artifactId>modelo0</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.5</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.4.Final</version>
</dependency>
<dependency>
<groupId>br.com.caelum</groupId>
<artifactId>vraptor</artifactId>
<version>3.5.3</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
</project>