import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
public class BookDao {
EntityManagerFactory entityManagerFactory;
@PersistenceContext
private EntityManager em;
public BookDao() {
System.err.println("krok 1");
entityManagerFactory = Persistence.createEntityManagerFactory("myDatabase");
System.err.println("krok 2");
em = entityManagerFactory.createEntityManager();
System.err.println("krok 3");
}
public List<Book> getAllBooks() {
// TypedQuery<Book> q = em.createQuery("select title from Book", Book.class);
// List<Book> resultList = q.getResultList();
// return resultList;
return new ArrayList<>();
}
}
W linii Persistence.createEntityManagerFactory("myDatabase");
Dostaję błąd
java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
Dodam że samo Dao jest injektowane wcześnie w innej warstwie w klasie z adnotacją Stateless
Do bazy danych tu wykorzystuję persistante.xml w którym mam
<persistence 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_2_0.xsd"
version="2.0">
<persistence-unit name="myDatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="radek" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/biblioteka" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<!--<property name="hibernate.hbm2ddl.auto" value="create" />-->
</properties>
</persistence-unit>
</persistence>
Całość kodu jest to https://github.com/cytryna/zielonaBiblioteka
W jaki sposób mam prawidłowo skonfigurować połączenie do bazy danych ?