Affectation Attribut entre de table ( oneToMany et ManyToOne )
Fermé
montisero
Messages postés65Date d'inscriptionjeudi 4 septembre 2014StatutMembreDernière intervention18 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;
}
}
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.
A voir également:
Affectation Attribut entre de table ( oneToMany et ManyToOne )