View Full Version : problema jpa+cdi relazioni l errore è ORA-02291: integrity constraint (ALLENAVITA.FK_
gaiapuffo
22-11-2013, 15:16
cavolo ,ho questo problema praticamente ho un entità utente e poi un entità paziente con una relazione one-to-many e many-to-one,praticamente io vado ad eseguire la funzione register...Vado a rendere persistenti sia utente che paziente...Poi se provo ad inserire utente e paziente e poi inserisco la referenza in utente per il paziente,non mi da errore e me la crea,ma se metto sotto u.setPazientes(p); metto
paziente1.setUtente(u); che sarebbe per inserire in paziente la referenza ad utente mi da ORA-02291: integrity constraint (ALLENAVITA.FK_PAZIENTE_UTENTE) violated - parent key not found
//cosi non mi da errore
System.out.println("Sto per fare il persist");
Set<Paziente> settaggiopaz=new HashSet<Paziente>();
settaggiopaz.add(paziente1);
System.out.println("Sto per persistere l'utente");
entityManager.persist(u);
entityManager.persist(paziente1);
Set <Paziente> p=new HashSet();
p.add(paziente1);
System.out.println("Controlla paziente");
System.out.println(paziente1.getCodiceSanitario()) ;
u.setPazientes(p);
//Cosi mi da errore
public void registerPaziente(Paziente paziente1,Utente u) throws NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException, InterruptedException {
System.out.println("Sto per fare il persist");
Set<Paziente> settaggiopaz=new HashSet<Paziente>();
settaggiopaz.add(paziente1);
System.out.println("Sto per persistere l'utente");
entityManager.persist(u);
entityManager.persist(paziente1);
Set <Paziente> p=new HashSet();
p.add(paziente1);
System.out.println("Controlla paziente");
System.out.println(paziente1.getCodiceSanitario()) ;
u.setPazientes(p);
paziente1.setUtente(u);
mone.java
23-11-2013, 07:53
Ad occhio e croce il tuo codice è un po caotico (usare il tag [ CODE ] aiuterebbe)? poi perché aggiungi lo stesso paziente a due set? E sarebbe anche utile avere il codice delle classi @Entity annotate.
gaiapuffo
23-11-2013, 09:30
Ciao,hai ragione la lista e solo un duplicato e ho sbagliato a inserirlo. Qui ho cercato ,di spiegarmi meglio:
Entità Utente:
private Set<Paziente> pazientes = new HashSet(0);
@OneToMany(fetch=FetchType.LAZY,mappedBy = "utente")
public Set<Paziente> getPazientes() {
return this.pazientes;
}
public void setPazientes(Set<Paziente> pazientes) {
this.pazientes = pazientes;
}
Entità paziente:
Utente user;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "UTENTE")
public Utente getUtente() {
return this.utente;
}
public void setUtente(Utente utente) {
this.utente = utente;
}
Invoco per rendere persistente e per creare le relazioni, la funzione register di RegistrationService:
@Stateful
@LocalBean
public class RegistrationService implements Serializable {
@PersistenceContext
public EntityManager entityManager;
public void register(Paziente paziente1,Utente u) {
// vado a salvare nel database l'utente e il paziente
in questo modo le entità dovrebbero diventare attached
entityManager.persist(u);
entityManager.persist(paziente1);
// vado recuperare la lista dei pazienti dal set Pazienti dell'entità Utente e ci aggiungo il nuovo paziente1
Set <Paziente> p=newHashset<Paziente>();
p.add(paziente1);
// Ho aggiornata il set di Pazienti di Utente e vado a fare il set per salvare tale modifica,essendo l'entità attached dopo il persist,l'
inserimento dovrebbe essere fatto nel db
u.setPazientes(p);
// Ora devo assegnare all'Entità Paziente la referenza all'Utente e quindi
paziente1.setUtente(u);
Essendo le entità attached per via delle persistenza dovrebbe in automatico inserire le referenze nel db ma mi da l'errore citato
ORA-02291: integrity constraint (ALLENAVITA.FK_PAZIENTE_UTENTE) violated - parent key not found
Ho anche un altro problema,in teoria io non devo crearmi una nuova lista e poi settarla ad utente,supponendo che utente abbia già dei pazienti,dovrei farmi dare il set di utente e poi aggiungere il nuovo paziente:
Set <Paziente> p=u.getPazientes();
p.add(paziente1);
Ma cosi mi da il seguente errore,anche se metto questa istruzione ad esempio all'inizio della funzione register,
11:01:19,956 INFO [org.jboss.as.ejb3] (http-localhost-127.0.0.1-8081-1) JBAS014101: Failed to find SFSB instance with session ID {[56, 114, 25, -26, 22, 121, 71, -75, -116, -71, 44, 96, 67, 118, 80, -81]} in cache
11:01:19,958 ERROR [org.jboss.ejb3.invocation] (http-localhost-127.0.0.1-8081-1) JBAS014134: EJB Invocation failed on component RegistrationService for method public void it.ictdays.javahostel.control.RegistrationService.registerPaziente(bean.Paziente,bean.Utente) throws javax.transaction.NotSupportedException,javax.transaction.SystemException,java.lang.SecurityException,java.lang.IllegalStateException,javax.transaction.RollbackException,javax.transaction.HeuristicMixedException,javax.transaction.HeuristicRollbackException,java.lang.InterruptedException: javax.ejb.EJBException: java.lang.NullPointerException
mone.java
23-11-2013, 09:58
Le relazioni le devi impostare prima! Cioè:
assumiamo che ne paziente1 e u siano stati scritti sul db...
Utente utente = new Utente();
utente.setPazientes(new HashSet<>());
em.flush(); //cosi utente viene aggiornato con l'id creato
Paziente paziente = new Paziente();
paziente.setUtente(utente);
em.persist(paziente);
u.getPazientes().add(paziente);
prova cosi ma non ti assicuro niente... dovrei fare 2 prove... ma la cosa migliore è mettere CascadeType.ALL in Utente... aggiungerci il paziente e fare il persist solo dell'utente in modo che il persist venga propagato anche al paziente.
gaiapuffo
23-11-2013, 10:29
Ho risolto il secondo errore,dichiarando la classe Stateless e il primo errore rimane..Ma se dichiaro la classe Stateless
mone.java
23-11-2013, 10:51
prova cosi:
@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL, mappedBy = "utente")
public Set<Paziente> getPazientes() {
return this.pazientes;
}
--------
Utente utente = new Utente();
utente.setPazientes(new HashSet<>());
Paziente paziente = new Paziente();
paziente.setUtente(utente);
u.getPazientes().add(paziente);
em.persist(utente);
prova a rimettere Stateful (se hai veramente bisogno si un bean Stateful) e riavviare il server.
gaiapuffo
23-11-2013, 13:58
intanto gentilissimo per le risposte...Allora il metodo cascate,l'ho provato ad usare anche io solo che se faccio
u.setPaziente(inserisco lista con paziente1) e poi faccio
entityManager(u);
Praticamente inserisce nel database,l'utente e il paziente,l'utente ha una referenza per raggiungere il paziente,ma il paziente ha il campo utente settato a null e quindi da paziente,nn posso risalire all'utente.
Ho quindi provato il codice da te scritto,ossia utilizzare cascate e settare le referenze per il paziente e utente
u.setPaziente(lista con paziente1)
paziente1.setUtente(u)
entityManager.persist(u)
Il problema e che mi da questo errore integrity constraint (ALLENAVITA.FK_PAZIENTE_UTENTE) violated - parent key not found
Ci sto dietro da 3 giorni e non riesco a venirne fuori,ho provato tutte le possibili combinazioni,ossia a settare prima le referenze e poi fare il persist di utente(usando cascade), a fare prima il persist e poi settare la referenza a paziente,ma nulla
mone.java
23-11-2013, 14:05
intanto gentilissimo per le risposte...Allora il metodo cascate,l'ho provato ad usare anche io solo che se faccio
u.setPaziente(inserisco lista con paziente1) e poi faccio
entityManager(u);
Praticamente inserisce nel database,l'utente e il paziente,l'utente ha una referenza per raggiungere il paziente,ma il paziente ha il campo utente settato a null e quindi da paziente,nn posso risalire all'utente.
Ho quindi provato il codice da te scritto,ossia utilizzare cascate e settare le referenze per il paziente e utente
u.setPaziente(lista con paziente1)
paziente1.setUtente(u)
entityManager.persist(u)
Il problema e che mi da questo errore integrity constraint (ALLENAVITA.FK_PAZIENTE_UTENTE) violated - parent key not found
Ci sto dietro da 3 giorni e non riesco a venirne fuori,ho provato tutte le possibili combinazioni,ossia a settare prima le referenze e poi fare il persist di utente(usando cascade), a fare prima il persist e poi settare la referenza a paziente,ma nulla
Dunque mandami per intero le tue classi entity! Vorrei vedere come hai impostato gli id! [possibilmente dentro ai tag CODE ]
gaiapuffo
23-11-2013, 14:51
@Entity
@Table(name = "UTENTE", schema = "AllenaVita")
public class Utente implements java.io.Serializable {
private int id;
private String nome;
private String cognome;
private Boolean amministratore;
private String email;
private String login;
private String password;
private boolean ruolo;
private Date scadenzaPwd;
private Date ultimoAccesso;
private Boolean stato;
private String telefonoCasa;
private String cellulare;
private String codiceAttivazione;
private boolean validato;
private Date lastOnline;
private String codiceFiscale;
private Date dataInserimento;
private Date dataNascita;
private Set<Attivita> attivitasForChiAttiva = new HashSet(0);
private Set<Paziente> pazientes = new HashSet(0);
private Set<Caregiver> caregivers = new HashSet(0);
private Set<Sottobiettivo> sottobiettivosForChiAttiva = new HashSet(0);
private Set<Nota> notas = new HashSet(0);
private Set <Infermiere>infermieres = new HashSet(0);
private Set<CollaboratoreStudio> collaboratoreStudios = new HashSet(0);
private Set <IndirizzoUtente>indirizzoUtentes = new HashSet(0);
private Set <Attivita>attivitasForChiChiude = new HashSet(0);
private Set <Sottobiettivo>sottobiettivosForChiChiude = new HashSet(0);
private Set <Valutazione>valutaziones = new HashSet(0);
private Set <Medico>medicos = new HashSet(0);
public Utente() {
}
public Utente(int id, String nome, String cognome, String login,
String password, boolean ruolo, String codiceAttivazione,
boolean validato) {
this.id = id;
this.nome = nome;
this.cognome = cognome;
this.login = login;
this.password = password;
this.ruolo = ruolo;
this.codiceAttivazione = codiceAttivazione;
this.validato = validato;
}
public Utente(int id, String nome, String cognome, Boolean amministratore,
String email, String login, String password, boolean ruolo,
Date scadenzaPwd, Date ultimoAccesso, Boolean stato,
String telefonoCasa, String cellulare, String codiceAttivazione,
boolean validato, Date lastOnline, String codiceFiscale,
Date dataInserimento, Date dataNascita, Set<Attivita> attivitasForChiAttiva,
Set<Paziente> pazientes, Set<Caregiver> caregivers, Set<Sottobiettivo> sottobiettivosForChiAttiva,
Set<Nota> notas, Set<Infermiere> infermieres, Set<CollaboratoreStudio> collaboratoreStudios,
Set<IndirizzoUtente> indirizzoUtentes, Set <Attivita> attivitasForChiChiude,
Set <Sottobiettivo>sottobiettivosForChiChiude, Set <Valutazione> valutaziones, Set <Medico>medicos) {
this.id = id;
this.nome = nome;
this.cognome = cognome;
this.amministratore = amministratore;
this.email = email;
this.login = login;
this.password = password;
this.ruolo = ruolo;
this.scadenzaPwd = scadenzaPwd;
this.ultimoAccesso = ultimoAccesso;
this.stato = stato;
this.telefonoCasa = telefonoCasa;
this.cellulare = cellulare;
this.codiceAttivazione = codiceAttivazione;
this.validato = validato;
this.lastOnline = lastOnline;
this.codiceFiscale = codiceFiscale;
this.dataInserimento = dataInserimento;
this.dataNascita = dataNascita;
this.attivitasForChiAttiva = attivitasForChiAttiva;
this.pazientes = pazientes;
this.caregivers = caregivers;
this.sottobiettivosForChiAttiva = sottobiettivosForChiAttiva;
this.notas = notas;
this.infermieres = infermieres;
this.collaboratoreStudios = collaboratoreStudios;
this.indirizzoUtentes = indirizzoUtentes;
this.attivitasForChiChiude = attivitasForChiChiude;
this.sottobiettivosForChiChiude = sottobiettivosForChiChiude;
this.valutaziones = valutaziones;
this.medicos = medicos;
}
@Id
@Column(name = "ID", unique = true, nullable = false, precision = 9, scale = 0)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "NOME", nullable = false, length = 50)
public String getNome() {
return this.nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Column(name = "COGNOME", nullable = false, length = 50)
public String getCognome() {
return this.cognome;
}
public void setCognome(String cognome) {
this.cognome = cognome;
}
@Column(name = "AMMINISTRATORE", precision = 1, scale = 0)
public Boolean getAmministratore() {
return this.amministratore;
}
public void setAmministratore(Boolean amministratore) {
this.amministratore = amministratore;
}
@Column(name = "EMAIL", length = 50)
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name = "LOGIN", nullable = false, length = 50)
public String getLogin() {
return this.login;
}
public void setLogin(String login) {
this.login = login;
}
@Column(name = "PASSWORD", nullable = false, length = 50)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "RUOLO", nullable = false, precision = 1, scale = 0)
public boolean isRuolo() {
return this.ruolo;
}
public void setRuolo(boolean ruolo) {
this.ruolo = ruolo;
}
@Temporal(TemporalType.DATE)
@Column(name = "SCADENZA_PWD", length = 7)
public Date getScadenzaPwd() {
return this.scadenzaPwd;
}
public void setScadenzaPwd(Date scadenzaPwd) {
this.scadenzaPwd = scadenzaPwd;
}
@Temporal(TemporalType.DATE)
@Column(name = "ULTIMO_ACCESSO", length = 7)
public Date getUltimoAccesso() {
return this.ultimoAccesso;
}
public void setUltimoAccesso(Date ultimoAccesso) {
this.ultimoAccesso = ultimoAccesso;
}
@Column(name = "STATO", precision = 1, scale = 0)
public Boolean getStato() {
return this.stato;
}
public void setStato(Boolean stato) {
this.stato = stato;
}
@Column(name = "TELEFONO_CASA", length = 10)
public String getTelefonoCasa() {
return this.telefonoCasa;
}
public void setTelefonoCasa(String telefonoCasa) {
this.telefonoCasa = telefonoCasa;
}
@Column(name = "CELLULARE", length = 10)
public String getCellulare() {
return this.cellulare;
}
public void setCellulare(String cellulare) {
this.cellulare = cellulare;
}
@Column(name = "CODICE_ATTIVAZIONE", nullable = false, length = 50)
public String getCodiceAttivazione() {
return this.codiceAttivazione;
}
public void setCodiceAttivazione(String codiceAttivazione) {
this.codiceAttivazione = codiceAttivazione;
}
@Column(name = "VALIDATO", nullable = false, precision = 1, scale = 0)
public boolean isValidato() {
return this.validato;
}
public void setValidato(boolean validato) {
this.validato = validato;
}
@Temporal(TemporalType.DATE)
@Column(name = "LAST_ONLINE", length = 7)
public Date getLastOnline() {
return this.lastOnline;
}
public void setLastOnline(Date lastOnline) {
this.lastOnline = lastOnline;
}
@Column(name = "CODICE_FISCALE", length = 16)
public String getCodiceFiscale() {
return this.codiceFiscale;
}
public void setCodiceFiscale(String codiceFiscale) {
this.codiceFiscale = codiceFiscale;
}
@Temporal(TemporalType.DATE)
@Column(name = "DATA_INSERIMENTO", length = 7)
public Date getDataInserimento() {
return this.dataInserimento;
}
public void setDataInserimento(Date dataInserimento) {
this.dataInserimento = dataInserimento;
}
@Temporal(TemporalType.DATE)
@Column(name = "DATA_NASCITA", length = 7)
public Date getDataNascita() {
return this.dataNascita;
}
public void setDataNascita(Date dataNascita) {
this.dataNascita = dataNascita;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "utenteByChiAttiva")
public Set<Attivita> getAttivitasForChiAttiva() {
return this.attivitasForChiAttiva;
}
public void setAttivitasForChiAttiva(Set<Attivita> attivitasForChiAttiva) {
this.attivitasForChiAttiva = attivitasForChiAttiva;
}
@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL,mappedBy = "utente")
public Set<Paziente> getPazientes() {
return this.pazientes;
}
public void setPazientes(Set<Paziente> pazientes) {
this.pazientes = pazientes;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "utente")
public Set<Caregiver> getCaregivers() {
return this.caregivers;
}
public void setCaregivers(Set<Caregiver> caregivers) {
this.caregivers = caregivers;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "utenteByChiAttiva")
public Set <Sottobiettivo>getSottobiettivosForChiAttiva() {
return this.sottobiettivosForChiAttiva;
}
public void setSottobiettivosForChiAttiva(Set <Sottobiettivo>sottobiettivosForChiAttiva) {
this.sottobiettivosForChiAttiva = sottobiettivosForChiAttiva;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "utente")
public Set<Nota> getNotas() {
return this.notas;
}
public void setNotas(Set<Nota> notas) {
this.notas = notas;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "utente")
public Set<Infermiere> getInfermieres() {
return this.infermieres;
}
public void setInfermieres(Set<Infermiere> infermieres) {
this.infermieres = infermieres;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "utente")
public Set<CollaboratoreStudio> getCollaboratoreStudios() {
return this.collaboratoreStudios;
}
public void setCollaboratoreStudios(Set<CollaboratoreStudio> collaboratoreStudios) {
this.collaboratoreStudios = collaboratoreStudios;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "utente")
public Set<IndirizzoUtente> getIndirizzoUtentes() {
return this.indirizzoUtentes;
}
public void setIndirizzoUtentes(Set<IndirizzoUtente> indirizzoUtentes) {
this.indirizzoUtentes = indirizzoUtentes;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "utenteByChiChiude")
public Set <Attivita>getAttivitasForChiChiude() {
return this.attivitasForChiChiude;
}
public void setAttivitasForChiChiude(Set<Attivita> attivitasForChiChiude) {
this.attivitasForChiChiude = attivitasForChiChiude;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "utenteByChiChiude")
public Set<Sottobiettivo> getSottobiettivosForChiChiude() {
return this.sottobiettivosForChiChiude;
}
public void setSottobiettivosForChiChiude(Set <Sottobiettivo>sottobiettivosForChiChiude) {
this.sottobiettivosForChiChiude = sottobiettivosForChiChiude;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "utente")
public Set<Valutazione> getValutaziones() {
return this.valutaziones;
}
public void setValutaziones(Set<Valutazione> valutaziones) {
this.valutaziones = valutaziones;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "utente")
public Set<Medico> getMedicos() {
return this.medicos;
}
public void setMedicos(Set<Medico> medicos) {
this.medicos = medicos;
}
}
package bean;
// Generated 8-nov-2013 12.01.29 by Hibernate Tools 3.4.0.CR1
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
/**
* Paziente generated by hbm2java
*/
@Entity
@Table(name = "PAZIENTE", schema = "AllenaVita")
public class Paziente implements java.io.Serializable {
private String idAna;
private Utente utente;
private Boolean sesso;
private Double altezza;
private String etnia;
private String luogoNascita;
private String codiceSanitario;
private String esenzioni;
private Boolean visualizzaRischio;
private String stato;
private Set<CartellaClinica> cartellaClinicas = new HashSet(0);
public Paziente() {
}
public Paziente(String idAna) {
this.idAna = idAna;
}
public Paziente(String idAna, Utente utente, Boolean sesso, Double altezza,
String etnia, String luogoNascita, String codiceSanitario,
String esenzioni, Boolean visualizzaRischio, String stato,
Set<CartellaClinica> cartellaClinicas) {
this.idAna = idAna;
this.utente = utente;
this.sesso = sesso;
this.altezza = altezza;
this.etnia = etnia;
this.luogoNascita = luogoNascita;
this.codiceSanitario = codiceSanitario;
this.esenzioni = esenzioni;
this.visualizzaRischio = visualizzaRischio;
this.stato = stato;
this.cartellaClinicas = cartellaClinicas;
}
@Id
@Column(name = "ID_ANA", unique = true, nullable = false, length = 50)
public String getIdAna() {
return this.idAna;
}
public void setIdAna(String idAna) {
this.idAna = idAna;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "UTENTE")
public Utente getUtente() {
return this.utente;
}
public void setUtente(Utente utente) {
this.utente = utente;
}
@Column(name = "SESSO", precision = 1, scale = 0)
public Boolean getSesso() {
return this.sesso;
}
public void setSesso(Boolean sesso) {
this.sesso = sesso;
}
@Column(name = "ALTEZZA", precision = 126, scale = 0)
public Double getAltezza() {
return this.altezza;
}
public void setAltezza(Double altezza) {
this.altezza = altezza;
}
@Column(name = "ETNIA", length = 50)
public String getEtnia() {
return this.etnia;
}
public void setEtnia(String etnia) {
this.etnia = etnia;
}
@Column(name = "LUOGO_NASCITA", length = 50)
public String getLuogoNascita() {
return this.luogoNascita;
}
public void setLuogoNascita(String luogoNascita) {
this.luogoNascita = luogoNascita;
}
@Column(name = "CODICE_SANITARIO", length = 16)
public String getCodiceSanitario() {
return this.codiceSanitario;
}
public void setCodiceSanitario(String codiceSanitario) {
this.codiceSanitario = codiceSanitario;
}
@Column(name = "ESENZIONI", length = 200)
public String getEsenzioni() {
return this.esenzioni;
}
public void setEsenzioni(String esenzioni) {
this.esenzioni = esenzioni;
}
@Column(name = "VISUALIZZA_RISCHIO", precision = 1, scale = 0)
public Boolean getVisualizzaRischio() {
return this.visualizzaRischio;
}
public void setVisualizzaRischio(Boolean visualizzaRischio) {
this.visualizzaRischio = visualizzaRischio;
}
@Column(name = "STATO", length = 50)
public String getStato() {
return this.stato;
}
public void setStato(String stato) {
this.stato = stato;
}
@OneToMany(cascade =CascadeType.ALL,fetch = FetchType.LAZY)
public Set<CartellaClinica> getCartellaClinicas() {
return this.cartellaClinicas;
}
public void setCartellaClinicas(Set<CartellaClinica> cartellaClinicas) {
this.cartellaClinicas = cartellaClinicas;
}
}
gaiapuffo
23-11-2013, 14:52
Le classi le ho trasferite attraverso hibernate in automatico,esportando un database oracle che ha fatto il mio compagno di gruppo che ha fatto solo quello. Purtroppo,sto facendo uno stage,dove nessuno sa aiutarmi perchè non ha conoscenze e devo fare tutto da solo e su questo punto mi sono bloccato
mone.java
23-11-2013, 14:57
prova a mettere sopra agli ID la seguente annotazione:
@GeneratedValue(strategy = GenerationType.IDENTITY)
esempio di calsse entità (hashCode ed equals non sono implementati nel migliore dei modi)
@Entity
@Table(name = "ORDINI")
public class Ordine {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String codice;
private Cliente cliente;
private Integer codiceCig;
@Temporal(TemporalType.DATE)
private Date dataOrdine;
@Temporal(TemporalType.DATE)
private Date dataConsegna;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "ordine")
private List<DipendenteOrdine> dipendenti;
private String note;
public Ordine() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCodice() {
return codice;
}
public void setCodice(String codice) {
this.codice = codice;
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
public Date getDataOrdine() {
return dataOrdine;
}
public void setDataOrdine(Date dataOrdine) {
this.dataOrdine = dataOrdine;
}
public Date getDataConsegna() {
return dataConsegna;
}
public void setDataConsegna(Date dataConsegna) {
this.dataConsegna = dataConsegna;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public List<DipendenteOrdine> getDipendenti() {
if (dipendenti == null) {
dipendenti = new ArrayList<>();
}
return dipendenti;
}
public void setDipendenti(List<DipendenteOrdine> dipendenti) {
this.dipendenti = dipendenti;
}
public Integer getCodiceCig() {
return codiceCig;
}
public void setCodiceCig(Integer codiceCig) {
this.codiceCig = codiceCig;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Ordine other = (Ordine) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
mone.java
23-11-2013, 15:00
Le classi le ho trasferite attraverso hibernate in automatico,esportando un database oracle che ha fatto il mio compagno di gruppo che ha fatto solo quello. Purtroppo,sto facendo uno stage,dove nessuno sa aiutarmi perchè non ha conoscenze e devo fare tutto da solo e su questo punto mi sono bloccato
si ho notato che c'è lo zampino di hibernate... a me sinceramente non piacciono le annotazioni sulla firma del metodo... le preferisco sul campo.. e poi appunto se non gli dai una strategia per generare l'id lui non lo genera e giustamente si incazza! :)
mone.java
24-11-2013, 19:50
hai risolto?
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.