Partage
  • Partager sur Facebook
  • Partager sur Twitter

Gestion de base de données avec Glassfish

Sujet résolu
    21 décembre 2015 à 11:13:48

    Bonjour,

    Actuellement apprenti en entreprise, je dois faire la liaison entre un serveur et une base de données. J'utilise Eclipse IDE pour le faire.

    J'ai réalisé plusieurs programmes que je vais joindre.

    Je préfère prévenir, je n'ai pas écris tout ce code à l'origine, j'ai pris sur des classes déjà existante en l'adaptant à mes besoins.

    D'après mon tuteur, les 3 premiers programmes sont bons mais quand je les appellent dans le xhtml j'ai des erreurs que je n'arrive pas à comprendre.

    Ma classe entité :

    package gmaoMoulage.entities;
    
    import java.io.Serializable;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.NamedQueries;
    import javax.persistence.NamedQuery;
    import javax.persistence.Table;
    
    
    /**
     * 
     * @author pgnansounou
     * Entity implementation class for Entity : Communication
     *
     */
    
    	@NamedQueries({
    			@NamedQuery(name="sqlCommunicationParIdCommunication", query="SELECT p FROM Communication p WHERE p.idCommunication=:idCommunication"),
    			@NamedQuery(name="sqlCommunicationParNomCommunication", query="SELECT p FROM Communication p WHERE p.nomCommunication=:nomCommunication"),
    			@NamedQuery(name="sqlCommunicationParEtatCommunication", query="SELECT p FROM Communication p WHERE p.etatCommunication=:etatCommunication")
    	})
    	
    
    @Entity
    @Table(name = "Communication")
    public class Communication implements Serializable { 
    
    		private static final long serialVersionUID = 1L;
    		
    		
    
    		@Id
    		@GeneratedValue( strategy = GenerationType.IDENTITY)
    		@Column(name= "idCommunication")
    		private Long idCommunication;
    		
    		@Column(name= "nomCommunication")
    		private String nomCommunication;
    		
    		@Column(name= "etatCommunication")
    		private String etatCommunication;
    
    		public Long getIdCommunication() {
    			return idCommunication;
    		}
    
    		public void setIdCommunication(Long idCommunication) {
    			this.idCommunication = idCommunication;
    		}
    
    		public String getNomCommunication() {
    			return nomCommunication;
    		}
    
    		public void setNomCommunication(String nomCommunication) {
    			this.nomCommunication = nomCommunication;
    		}
    
    		public String getEtatCommunication() {
    			return etatCommunication;
    		}
    
    		public void setEtatCommunication(String etatCommunication) {
    			this.etatCommunication = etatCommunication;
    		}
    		
    }
    

    Mon DAO :

    package gmaoMoulage.dao;
    
    import gmaoMoulage.dao.global.DAOException;
    import gmaoMoulage.entities.Communication;
    import java.util.List;
    
    import javax.ejb.Stateless;
    import javax.persistence.EntityManager;
    import javax.persistence.NoResultException;
    import javax.persistence.PersistenceContext;
    import javax.persistence.Query;
    import javax.persistence.TypedQuery;
    
    
    @Stateless
    public class CommunicationDao {
    
    	@PersistenceContext( unitName = "bdd_GMAOMoulage" )
    	private EntityManager em;
    	
    	private static final String PARAM_ID_COMMUNICATION = "idCommunication";
    	private static final String PARAM_NOM_COMMUNICATION = "nomCommunication";
    	private static final String PARAM_ETAT_COMMUNICATION = "etatCommunication";
    	
    	public void ajouter_communication( Communication communication ) throws DAOException {
    		try {
    			em.persist( communication );
    		} catch ( Exception e ) {
    			throw new DAOException( e );
    		}
    	}
    	
    	public List<Communication> afficher_communication() throws DAOException {
    		
    		try {				
    				TypedQuery<Communication> sql = em.createNamedQuery("sqlCommunicationAll", Communication.class);
    				return sql.getResultList();
    			} catch ( DAOException e ) {
    				throw new DAOException( e );
    			}
    		}
    	
    public Communication recherche_communication_id ( long idCommunication ) throws DAOException {
    		
    		Communication communication = null;
    		
    		Query sql1 = em.createNamedQuery("sqlCommunicationParIdCommunication");
    		sql1.setParameter(PARAM_ID_COMMUNICATION, idCommunication);
    		
    		try {
    			
    			communication = (Communication) sql1.getSingleResult();
    		}catch ( NoResultException e) {
    			return null;
    		} catch ( Exception e ) {
    			throw new DAOException( e );
    		}
    		return communication;
    	}
    	
    public Communication recherche_communication_nom ( String nomCommunication ) throws DAOException {
    	
    	Communication communication = null;
    	
    	Query sql1 = em.createNamedQuery("sqlCommunicationParNonCommunication");
    	sql1.setParameter(PARAM_NOM_COMMUNICATION, nomCommunication);
    	
    	try {
    		
    		communication = (Communication) sql1.getSingleResult();
    	}catch ( NoResultException e) {
    		return null;
    	} catch ( Exception e ) {
    		throw new DAOException( e );
    	}
    	return communication;
    }
    
    
    public Communication recherche_communication_etat ( String etatCommunication ) throws DAOException {
    	
    	Communication communication = null;
    	
    	Query sql1 = em.createNamedQuery("sqlCommunicationParEtatCommunication");
    	sql1.setParameter(PARAM_ETAT_COMMUNICATION, etatCommunication);
    	
    	try {
    		
    		communication = (Communication) sql1.getSingleResult();
    	}catch ( NoResultException e) {
    		return null;
    	} catch ( Exception e ) {
    		throw new DAOException( e );
    	}
    	return communication;
    }
    }

    Mon code pour ajouter :

    package gmaoMoulage.forms;
    
    
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.ejb.EJB;
    import javax.faces.application.FacesMessage;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;
    import javax.faces.context.FacesContext;
    
    import gmaoMoulage.dao.CommunicationDao;
    import gmaoMoulage.entities.Communication;
    
    
    @ManagedBean
    @ViewScoped
    
    
    public class AjouterCommunication  implements Serializable {
    
    	private static final long serialVersionUID = 1L;
    	private List<Communication> listeCommunication = new ArrayList<Communication>();
    
    	private Communication communication;
    
    	// Injection de notre EJB (Session Bean Stateless)
    	@EJB
    	private CommunicationDao communicationDao;
    	
    	// Initialisation de l'entité utilisateur
    
    
    	public AjouterCommunication() {
    		communication = new Communication();
    	}
    	
    	// Méthode d'action appelée lors du clic sur le bouton duformulaire
    	public void ajouter_communication() {
    		communicationDao.ajouter_communication(communication);
    		communication = new Communication();
    		FacesMessage message = new FacesMessage( "Succès de	l'inscription !" );
    		FacesContext.getCurrentInstance().addMessage( null, message	);
    	}
    	
    	public Communication getCommunication(){
    		return communication;
    	}
    	
    	public List<Communication> getListeCommunication() {
    		/*int te = 3;*/
    		listeCommunication = communicationDao.afficher_communication();
    		return listeCommunication;
    	}
    
    	public CommunicationDao getCommunicationDao() {
    		return communicationDao;
    	}
    
    	public void setCommunicationDao(CommunicationDao communicationDao) {
    		this.communicationDao = communicationDao;
    	}
    
    	public void setListeCommunication(List<Communication> listeCommunication) {
    		this.listeCommunication = listeCommunication;
    	}
    
    	public void setCommunication(Communication communication) {
    		this.communication = communication;
    	}
    	
    }
    

    La partie de mon code XHTML qui ne fonctionne pas ( le reste non plus ne fonctionne pas mais je préfère déjà faire fonctionner celui-ci

    <b:panel title="Ajouter une communication" look="primary">	
    <h:form>
    <h:panelGrid  columns="3" cellpadding="5">
    <!-- Nom Communication -->
    <h:outputLabel for="ch_communication">Nom de Communication <span class="requis">*</span></h:outputLabel>
    <h:inputText id="ch_communication"	value="#{ajouterCommunication.communication.nomCommunication}">
    <f:ajax event="blur" render="ch_communicationMessage" />
    </h:inputText>
    <h:message id="ch_communicationMessage" for="ch_communication" errorClass="erreur" />
    				
    <!-- Etat Communication -->
    <h:outputLabel for="ch_etat_communication">Etat de la communication: <span class="requis">*</span></h:outputLabel>
    <h:selectOneMenu id="ch_etat_communication" value="#{ajouterCommunication.communication.etatCommunication}">
    <f:selectItem itemValue="True" itemLabel="True" />
    <f:selectItem itemValue="False" itemLabel="False" />
    <f:ajax event="blur" render="ch_etat_communicationMessage" />
    </h:selectOneMenu>
    <h:message id="ch_etat_communicationMessage" for="ch_etat_communication" errorClass="erreur" />
    				
    <!-- Button -->
    <b:button value="Annuler" look="danger"></b:button>
    <!-- Button Valider -->
    <b:commandButton value="Enregistrer" action="#{ajouterCommunication.ajouter_communication}" look="success" />
    </h:panelGrid>	
    <h:messages globalOnly="true" infoClass="info" />
    		</h:form>
    		</b:panel>

    Et mes données dans ma base, la table se nomne "Communication", j'utilise sql server au cas ou

    Erreur rencontrée quand je lance mon document xhtml :

    root cause

    javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Impossible d'insérer la valeur NULL dans la colonne 'idCommunication', table 'GMAOMoulageTest.dbo.Communication'. Cette colonne n'accepte pas les valeurs NULL. Échec de INSERT.
    Error Code: 515
    Call: INSERT INTO Communication (etatCommunication, nomCommunication) VALUES (?, ?)
    	bind => [2 parameters bound]
    Query: InsertObjectQuery(gmaoMoulage.entities.Communication@748a9589)

    root cause

    Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Impossible d'insérer la valeur NULL dans la colonne 'idCommunication', table 'GMAOMoulageTest.dbo.Communication'. Cette colonne n'accepte pas les valeurs NULL. Échec de INSERT.
    Error Code: 515
    Call: INSERT INTO Communication (etatCommunication, nomCommunication) VALUES (?, ?)
    	bind => [2 parameters bound]
    Query: InsertObjectQuery(gmaoMoulage.entities.Communication@748a9589)

    root cause

    com.microsoft.sqlserver.jdbc.SQLServerException: Impossible d'insérer la valeur NULL dans la colonne 'idCommunication', table 'GMAOMoulageTest.dbo.Communication'. Cette colonne n'accepte pas les valeurs NULL. Échec de INSERT.

    Merci de m'accorder votre temps.

    • Partager sur Facebook
    • Partager sur Twitter

    Gestion de base de données avec Glassfish

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown