Affectation Attribut entre de table ( oneToMany et ManyToOne )

Fermé
montisero Messages postés 65 Date d'inscription jeudi 4 septembre 2014 Statut Membre Dernière intervention 18 mars 2016 - Modifié par montisero le 25/03/2015 à 18:21
bonjour ,
j'ai la relation entre les deux table comme il est indiqué dessous .

- je veux affecter ip adress à un neighbor comme il est indiqué mais le probleme que j'arrive pas à recupérer l'ip adress dans la deuxième table (Switch neighbor )

voila mon code

List Switch.java

package persistence;

import java.io.Serializable;
import java.util.List;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;



@Entity

public class ListSwitchs implements Serializable {

 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)

 private int id ; 
 
 private String name ; 
 private String ip_address ; 
 private String description ;
 
 
 @OneToMany ( mappedBy ="listswitchs")
 private List<Neighbor> switchs;
 
 public ListSwitchs() {
  
 }


 
 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getIp_address() {
  return ip_address;
 }

 public void setIp_address(String ip_address) {
  this.ip_address = ip_address;
 }

 public String getDescription() {
  return description;
 }

 public void setDescription(String description) {
  this.description = description;
 }



 public List<Neighbor> getSwitchs() {
  return switchs;
 }



 public void setSwitchs(List<Neighbor> switchs) {
  this.switchs = switchs;
 }

 
 




 
 
 
 

}



neighbor.java

package persistence;

import java.io.Serializable;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Neighbor implements Serializable{

 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 

 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)

 private int id ;
 
 @ManyToOne
 @JoinColumn(name="Fk_Switch")
 private ListSwitchs listswitchs ;
 
 private String ip_address ;
 private String Interface ;
 private String ip_address_neighbor ;
 private String Port_ID ;
 
 public Neighbor() {
  // TODO Auto-generated constructor stub
 }
 

 public ListSwitchs getListswitchs() {
  return listswitchs;
 }
 public void setListswitchs(ListSwitchs listswitchs) {
  this.listswitchs = listswitchs;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getIp_address() {
  return ip_address;
 }
 public void setIp_address(String ip_address) {
  this.ip_address = ip_address;
 }
 public String getInterface() {
  return Interface;
 }
 public void setInterface(String interface1) {
  Interface = interface1;
 }
 public String getIp_address_neighbor() {
  return ip_address_neighbor;
 }
 public void setIp_address_neighbor(String ip_address_neighbor) {
  this.ip_address_neighbor = ip_address_neighbor;
 }
 public String getPort_ID() {
  return Port_ID;
 }
 public void setPort_ID(String port_ID) {
  Port_ID = port_ID;
 }
 
 

}



neighborService

package service;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import persistence.Neighbor;



/**
 * Session Bean implementation class SwitchServer
 */
@Stateless
@LocalBean
public class SwitchServer implements SwitchServerRemote {

    /**
     * Default constructor. 
     */
 
 @PersistenceContext
 EntityManager entityManager;
    public SwitchServer() {
        // TODO Auto-generated constructor stub
    }
 @SuppressWarnings("unchecked")
 @Override
 public List<Neighbor> getAll() {
  Query query = entityManager.createQuery("select  e from Neighbor e");
  return query.getResultList();
 }
 @Override
 public void insertNeighbor(Neighbor switchs) {
  entityManager.persist(switchs);
  
 }

}



ListSwitch service.java

package service;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import persistence.ListSwitchs;



/**
 * Session Bean implementation class ListSwitchService
 */
@Stateless
@LocalBean
public class ListSwitchService implements ListSwitchServiceRemote {

    /**
     * Default constructor. 
     */
 
 @PersistenceContext
 EntityManager entityManager;
    public ListSwitchService() {
       
    }

 @SuppressWarnings("unchecked")
 @Override
 public List<ListSwitchs> getAll() {
  Query query = entityManager.createQuery("select  e from ListSwitchs e");
  return query.getResultList();
 }

 @Override
 public ListSwitchs getByIp(String ip) {
  return (ListSwitchs) entityManager.createQuery("SELECT t FROM ListSwitchs t WHERE t.ip_address =:nm").setParameter("nm", ip).getSingleResult();
 }

 @Override
 public void insertSwitch(ListSwitchs listSwitchs) {
  entityManager.persist(listSwitchs);
  
 }

}



neighbor bean

package sungard_web.mbeans;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;










import persistence.ListSwitchs;
import persistence.Neighbor;
import service.ListSwitchServiceRemote;
import service.SwitchServerRemote;


@ManagedBean
@ViewScoped
public class neiborsBean implements Serializable{

 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 
 @EJB
 SwitchServerRemote switchServerRemote ;
 @EJB
 ListSwitchServiceRemote listSwitchServiceRemote ;
 
 

 private Neighbor selectedNeighbor ;
 private ListSwitchs selectedswitch  ; 
 
 
 private List <Neighbor> switchsss ;
 private Neighbor switchs ;
 
 
 
 public neiborsBean() {
 
 }

 @PostConstruct
  public void  init () {
  
   FacesContext.getCurrentInstance().getExternalContext().getSession(true);
  
 
   
   selectedswitch = new ListSwitchs();
   switchs = new Neighbor();
   selectedNeighbor=new Neighbor ();
   switchsss=switchServerRemote.getAll();
   
  }
 
 public void addNeighbor() {
  System.out.println("addNeighbor invoked ");

  switchServerRemote.insertNeighbor(switchs);
   switchsss=switchServerRemote.getAll();
  
  
 }
 
 public void reset() {

  this.switchs = new Neighbor();
 }

 public List <Neighbor> getSwitchsss() {
  return switchsss;
 }

 public Neighbor getSwitchs() {
  return switchs;
 }

 public void setSwitchs(Neighbor switchs) {
  this.switchs = switchs;
 }

 public void setSwitchsss(List<Neighbor> switchsss) {
  this.switchsss = switchsss;
 }

 public Neighbor getSelectedNeighbor() {
  return selectedNeighbor;
 }

 public void setSelectedNeighbor(Neighbor selectedNeighbor) {
  this.selectedNeighbor = selectedNeighbor;
 }

 public ListSwitchs getSelectedswitch() {
  return selectedswitch;
 }

 public void setSelectedswitch(ListSwitchs selectedswitch) {
  this.selectedswitch = selectedswitch;
 }

 

 
 
}



neighbor.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition template="/template/template.xhtml"
 xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:p="http://primefaces.org/ui"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:c="http://java.sun.com/jsp/jstl/core">


 <ui:define name="pageContent">
    <h:head>
        <title>CMDB</title>
        <link rel="stylesheet" href="css/style.css "/>
    </h:head>
    <h:body>
    
    
    <h:form id="a">
      <p:tabView dynamic="true" cache="true">
        <p:tab title="switchs">
            <h:panelGrid  cellpadding="10">
     <p:dataTable value="#{listSwitchBean.listSwitches}" var="item" id="tbl"   widgetVar="PersonneTable" filteredValue=""
     emptyMessage="No printer with given criteria" 
     selection="#{listSwitchBean.selectedSwitch}"
       editable="true" editmode="row"
       selectionMode="single" 
       rowKey="#{item.id}"
     paginator="true" paginatorPosition="bottom"
    rows="10" style="margin-bottom:5px"
    paginatorTemplate=" {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
    rowsPerPageTemplate="5,10,15">
    
 
                <f:facet name="header">
                    <p:outputPanel>
                        <h:outputText value="Search:" />
                        <p:inputText id="globalFilter" onkeyup="PersonneTable.filter()" style="width:130px" />
                        
                        
                    </p:outputPanel>
                </f:facet>
 
        <f:facet name="{Exporters}">
            <h:commandLink>
                <p:graphicImage value ="/photo/img/excel.png" width="35"/>
                <p:dataExporter type="xls" target="tbl" fileName="printer" />
            </h:commandLink>
 
            <h:commandLink>
                <p:graphicImage value="/photo/img/pdf.jpg" width="35"/>
                <p:dataExporter type="pdf" target="tbl" fileName="printer"/>
            </h:commandLink>
 
            <h:commandLink>
                <p:graphicImage value="/photo/img/csv.png" width="35"/>
                <p:dataExporter type="csv" target="tbl" fileName="printer" />
            </h:commandLink>
 
           
        </f:facet>
 
        <p:column filterBy ="#{item.description}">
            <f:facet name="header">
                <h:outputText value="Name" />
            </f:facet>
            <h:outputText value="#{item.description}" />
        </p:column>
 
       
 
        <p:column filterBy ="#{item.ip_address}">
            <f:facet name="header">
                <h:outputText value="IPAdress"  />
            </f:facet>
            <h:outputText value="#{item.ip_address}" />
        </p:column>
 
      
        
        <p:column filterBy ="#{item.name}">
            <f:facet name="header">
                <h:outputText value="Description" />
            </f:facet>
            <h:outputText value="#{item.name}" />
        </p:column>
        
        
      
        
          
           <p:column>
                   <p:commandLink update=":edit" oncomplete="userAjout.show()" title="View Detail">  
                    <h:outputText styleClass="ui-icon ui-icon-search" style="margin:0 auto;" />   
                    <f:setPropertyActionListener value="#{item}"   
                            target="#{listSwitchBean.selectedSwitch}" />  
                </p:commandLink>  
                  </p:column> 
        
    
        
     
        
       
    </p:dataTable>
     </h:panelGrid>
        </p:tab>
        
              <p:tab title="switchs neighbors">
            <h:panelGrid  cellpadding="10">
     <p:dataTable value="#{neiborsBean.switchsss}" var="item" id="mydata"   widgetVar="PersonneTable" filteredValue=""
     emptyMessage="No neighbor with given criteria" 
       editable="true" editmode="row"
       selectionMode="single" 
       selection="#{neiborsBean.selectedNeighbor}" 
       rowKey="#{item.id}"
     paginator="true" paginatorPosition="bottom"
    rows="10" style="margin-bottom:5px"
    paginatorTemplate=" {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
    rowsPerPageTemplate="5,10,15">
    
 
                <f:facet name="header">
                    <p:outputPanel>
                        <h:outputText value="Search:" />
                        <p:inputText id="globalFilter" onkeyup="PersonneTable.filter()" style="width:130px" />
                        
                        
                    </p:outputPanel>
                </f:facet>
 
        <f:facet name="{Exporters}">
            <h:commandLink>
                <p:graphicImage value ="/photo/img/excel.png" width="35"/>
                <p:dataExporter type="xls" target="tbl" fileName="printer" />
            </h:commandLink>
 
            <h:commandLink>
                <p:graphicImage value="/photo/img/pdf.jpg" width="35"/>
                <p:dataExporter type="pdf" target="tbl" fileName="printer"/>
            </h:commandLink>
 
            <h:commandLink>
                <p:graphicImage value="/photo/img/csv.png" width="35"/>
                <p:dataExporter type="csv" target="tbl" fileName="printer" />
            </h:commandLink>
 
           
        </f:facet>
        
       
 
        <p:column filterBy ="#{item.listswitchs.ip_address}">
            <f:facet name="header">
                <h:outputText value="Switch Adress" />
            </f:facet>
            <h:outputText value="#{item.listswitchs.ip_address}" />
        </p:column>
 
       
 
        <p:column filterBy ="#{item.interface}">
            <f:facet name="header">
                <h:outputText value="Interface"  />
            </f:facet>
            <h:outputText value="#{item.interface}" />
        </p:column>
 
      
        
        <p:column filterBy ="#{item.ip_address_neighbor}">
            <f:facet name="header">
                <h:outputText value="Neighbor Adress" />
            </f:facet>
            <h:outputText value="#{item.ip_address_neighbor}" />
        </p:column>
        
        <p:column filterBy ="#{item.port_ID}">
            <f:facet name="header">
                <h:outputText value="Port Id" />
            </f:facet>
            <h:outputText value="#{item.port_ID}" />
        </p:column>
        
      
    
        
     
        
       
    </p:dataTable>
     </h:panelGrid>
        </p:tab>
        </p:tabView>
 
    <h3>Export Page Data Only</h3>
    <h:commandLink>
        <p:graphicImage value ="/photo/img/excel.png" />
        <p:dataExporter type="xls" target="mydata" fileName="printer" pageOnly="true"/>
    </h:commandLink>
 
    <h:commandLink>
        <p:graphicImage value="/photo/img/pdf.jpg" />
        <p:dataExporter type="pdf" target="mydata" fileName="printer" pageOnly="true" />
    </h:commandLink>
 
    <h:commandLink>
        <p:graphicImage value="/photo/img/csv.png" />
        <p:dataExporter type="csv" target="mydata" fileName="printer" pageOnly="true" />
    </h:commandLink>
 
  
</h:form>


<h:form id="edit">
    <p:growl id="msgs" showDetail="true" sticky="false" /> 
              <p:dialog header="Affect to neighbor" widgetVar="userAjout" resizable="false" id="editP" >
    
                <h:panelGrid id="display" columns="2" cellpadding="6" style="margin:0 auto;">
                
                
     
     <h:outputLabel for="firstName" value="Enter IP: "  style="color: crimson;"/>
     <p:inputText id="firstNa" value="#{listSwitchBean.selectedSwitch.ip_address}"
     required="true" requiredMessage="Please Enter Id!" />

    <h:outputLabel for="firstName" value="Enter interface: "  style="color: crimson;"/>
     <p:inputText id="firstName" value="#{neiborsBean.switchs.interface}"
     required="true" requiredMessage="Please Enter interface!" />
     
    <h:outputLabel for="lastName" value="Enter user ip_address_neighbor : "  style="color: crimson;"/>
     <p:inputText id="lastName" value="#{neiborsBean.switchs.ip_address_neighbor}"
     required="true" requiredMessage="Please Enter ip_address_neighbor!" />
     
     <h:outputLabel for="role" value="Enter Port Id : "  style="color: crimson;"/>
     <p:inputText id="role" value="#{neiborsBean.switchs.port_ID}"
     required="true" requiredMessage="Please Enter Port Id!" />
     
     
    
             <p:growl id="messages" showDetail="true"/>  
              
               
    <p:commandButton action="#{neiborsBean.addNeighbor}"
      value="Save" icon="ui-icon-check" 
     style="margin:10px" oncomplete="userAjout.hide()"/>
     
        <!-- <p:message for="name" id="msg_name"/> -->
     
   </h:panelGrid>
  </p:dialog>
  </h:form>
    
    
    
    
    
    
    </h:body>
</ui:define>
</ui:composition>



images 1 (valeur de text ip adress dejà recuperer de la table ) et quand je clique "save" tout les autres s'ajout sauf ipadress qu'il est récupéré



image 2 : voila je veux qui'il ajoute l'adress recupéré dans la table switch dans la colone " switch adress" de dexieme table " switch neighbors " mais toujours vide :(


Cordialement.