Connexion à la base de données/Hibernate
Bonjour je cherche à gérer une connexion à une base de données mysql pour un projet java, j'ai crée mes entités, et les méthodes de connexion, ainsi que la table dont j'ai besoin pour effectuer la connexion, l'ennui c'est que je ne sais pas si la connexion a pu s'établir, visiblement ce n'est pas le cas car aucune de mes entités n'a été inséré dans la table voici mes bouts de code:
PersistenceManager.java:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class PersistenceManager extends Persistence {
private static EntityManagerFactory emf;
public PersistenceManager() {}
//création entité permettant de se connecter à la base de données.
public static EntityManagerFactory getEntityManagerFactory() {
if (emf == null) {
emf = Persistence.createEntityManagerFactory("PROJET-PU");
}
return emf;
}
//déco si on est connecté (dans ce cas l'entité de connexion n'est pas fermée)
public static void closeEntityManagerFactory(){
if (emf != null && emf.isOpen()) {
emf.close();
}
}
} |
persistence.xml:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="PROJET-PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:80/nom_projet" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<!-- ou update -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
</properties>
</persistence-unit>
</persistence> |
PersistenceAppListener.java:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public class PersistenceAppListener implements ServletContextListener {
public PersistenceAppListener() {
// TODO Auto-generated constructor stub
}
//fermeture du listener et donc des requêtes.
@Override
public void contextDestroyed(ServletContextEvent event) {
PersistenceManager.closeEntityManagerFactory();
}
@Override
public void contextInitialized(ServletContextEvent event) {
// TODO Auto-generated method stub
}
} |
DaoFactory.java:
Code:
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
| public final class DaoFactory {
private static DaoFactory instance;//création instance de la base de donnée,
//gestion des
private CampusDao campusDao;
private InterventionDao interventionDao;
private SpeakerDao speakerDao;
private StudentDao studentDao;
//création de l'instance contenue dans le getter
public static DaoFactory getInstance() {
if (instance == null) {
instance = new DaoFactory();
}
return instance;
}
public CampusDao getCampusDao() {
if (campusDao == null) {
campusDao = new JpaCampusDao(PersistenceManager.getEntityManagerFactory());
}
return campusDao;
}
public InterventionDao getInterventionDao() {
if (interventionDao == null) {
interventionDao = new JpaInterventionDao(PersistenceManager.getEntityManagerFactory());
}
return interventionDao;
}
public SpeakerDao getSpeakerDao() {
if (speakerDao == null) {
speakerDao = new JpaSpeakerDao(PersistenceManager.getEntityManagerFactory());
}
return speakerDao;
}
public StudentDao getStudentDao() {
if (studentDao == null) {
studentDao = new JpaStudentDao(PersistenceManager.getEntityManagerFactory());
}
return studentDao;
}
} |
et une Entité d'exemple: la table Campus
Code:
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
| @Entity
@Table(name = "CAMPUS")
public class Campus implements Serializable{
private static final long serialVersionUID = 1L;
@Id @GeneratedValue
private Long id;
@Column(unique=true, nullable=false)
private String name;
@ManyToMany
@JoinTable(name = "INTERVENTION")
private List<Intervention> interventions;
@ManyToOne
@JoinColumn
private Student student;
public Campus(String name) {
this.name = name;
}
public Campus() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Intervention> getInterventions() {
return interventions;
}
public void setInterventions(List<Intervention> interventions) {
this.interventions = interventions;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
} |
enfin fichier permettant de gérer la connexion via Jpa:
JpaCampusDao.java:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class JpaCampusDao implements CampusDao {
private EntityManagerFactory emf;
public JpaCampusDao(EntityManagerFactory emf) {
this.emf = emf;
}
@SuppressWarnings("unchecked")
@Override
public List<Campus> getAllCampuses() {
EntityManager em = emf.createEntityManager();
try {
Query query = em.createQuery("SELECT c FROM Campus c");
return query.getResultList();
} finally{
em.close();
}
}
} |
Je suis désolé d'avoir mis autant de code mais au moins il y a tout le nécessaire je pense. Donc j'aimerais savoir comment insérer en gros ma table Campus dans la base de donnée, j'utilise bien le port 80 en localhost pour phpmyadmin et la bonne table avec les bons identifiants et mots de passe mais l'insertion ne marche pas...
Je vous remercie d'avance ;)