Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
120 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package iti.note.model; | ||
|
||
import iti.note.jpa.EMF; | ||
import iti.note.jpa.ModelHelper; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.persistence.CascadeType; | ||
import javax.persistence.Entity; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.OneToMany; | ||
import javax.persistence.Table; | ||
import javax.persistence.TypedQuery; | ||
|
||
@Entity | ||
@Table | ||
public class User implements Serializable { | ||
|
||
private static final long serialVersionUID = -8337410006399411303L; | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private long id; | ||
|
||
private String nome; | ||
|
||
@OneToMany(cascade = CascadeType.ALL) | ||
@JoinColumn(name = "user_id") | ||
private List<Taccuino> taccuini = new ArrayList<Taccuino>(); | ||
|
||
public User() { | ||
// TODO Auto-generated constructor stub | ||
} | ||
|
||
public String getNome() { | ||
return nome; | ||
} | ||
|
||
public void setNome(String nome) { | ||
this.nome = nome; | ||
} | ||
|
||
public List<Taccuino> getTaccuini() { | ||
return taccuini; | ||
} | ||
|
||
public void setTaccuini(List<Taccuino> taccuini) { | ||
this.taccuini = taccuini; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package iti.note.test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import iti.note.model.Nota; | ||
import iti.note.model.Taccuino; | ||
import iti.note.model.User; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.Persistence; | ||
|
||
public class UserPersistenceTest { | ||
|
||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
|
||
EntityManagerFactory emf = Persistence | ||
.createEntityManagerFactory("myFirstPersistenceUnit"); | ||
|
||
EntityManager em = emf.createEntityManager(); | ||
|
||
Taccuino taccuino = new Taccuino("Il mio taccuino persistente"); | ||
Nota nota = new Nota("La mia nota persistente"); | ||
taccuino.addNota(nota); | ||
|
||
User u = new User(); | ||
u.setNome("Il mio nome"); | ||
|
||
List<Taccuino> taccuini = new ArrayList<Taccuino>(); | ||
taccuini.add(taccuino); | ||
|
||
u.setTaccuini(taccuini); | ||
|
||
em.getTransaction().begin(); | ||
|
||
em.persist(u); | ||
|
||
em.getTransaction().commit(); | ||
} | ||
} |