Merci plawyx mais pour H2 c'est déjà en place et nous devons utiliser celui du tuto du prof (connection profil sous eclipse).
Pour le persistence.xml, tu veux dire qu'il doit juste ce mettre au même endroit dans le projet jsf et jpa donc dans META-INF/ ?
Si oui, je viens de le faire donc ça, c'est fait. Maintenant j'ai le profil de connexion H2 et le persistance positionné.
Dans le projet jsf existant il y a un formulaire de connexion avec login/mdp.
La méthode lancé se trouve dans une classe UserService et est la suivante :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
@Override
public User logUser(String login, String password)
throws UserUnknownException, InvalidPasswordException {
boolean userExists = login.equals("jdoee");
boolean passwordValid = password.equals("jdoejdoe");
if (!userExists){
throw new UserUnknownException("No user " + login + " exists.", null);
}
if (!passwordValid){
throw new InvalidPasswordException("Password given in invalid for user " + login + ".", null);
}
User user = new User();
user.setEmail("jode@gmail.com");
user.setLogin(login);
user.setPassword(password);
return user;
} |
Hors moi je ne veux pas vérifier en dur mais belle et bien aller chercher dans ma base H2. Je présume donc que je dois créer mon entitymanager pour pouvoir faire un
em.find(User.class, login);
et comparer le mdp.
Mais je ne sais pas comment me connecter à ma base, est-ce automatique uniquement via le persistence.xml ou doit on créer une classe de connexion comme celle que nous avions eu pour nous connecter dans le projet 1 - JPA avec BDD dans fichier xml :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
package com.bookstore.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.XmlDataSet;
import org.dbunit.operation.DatabaseOperation;
public class TestUtils {
private static Connection connection;
private static IDataSet dbUnitDataset;
public static Connection getJDBCConnection(){
try{
if (connection == null){
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
connection = DriverManager.getConnection("jdbc:derby:memory:BookStoreDB;");
}
}catch(Exception e){
e.printStackTrace();
}
return connection;
}
public static void reloadDbUnitData() throws Exception{
Connection connection = getJDBCConnection();
PreparedStatement stmt = connection
.prepareStatement("ALTER TABLE AUTHORS ALTER COLUMN id RESTART WITH 1");
stmt.execute();
stmt.close();
/* load the first time only */
if (dbUnitDataset == null){
dbUnitDataset = new XmlDataSet(Thread.currentThread()
.getContextClassLoader().getResourceAsStream("data-dbunit.xml"));
}
// Clean the data from previous test and insert new data test.
DatabaseOperation.CLEAN_INSERT.execute(new DatabaseConnection(connection), dbUnitDataset);
connection.commit();
closeJDBCConnection();
}
public static void closeJDBCConnection(){
if (connection != null){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection = null;
}
}
} |
Partager