Browse files

Finish userentity

  • Loading branch information...
2 parents a7028a6 + 42a86e2 commit e510ade37ade573ea22bda62dc1660ea7ab27312 @alessandro-aglietti committed Feb 21, 2014
View
13 iti-note-model-jdbc-dao/src/iti/note/model/Taccuino.java
@@ -1,11 +1,5 @@
package iti.note.model;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.List;
public class Taccuino {
private long id;
@@ -16,15 +10,16 @@ public Taccuino() {
// TODO Auto-generated constructor stub
}
+ public long getId() {
+ return id;
+ }
+
public Taccuino(long id, String titolo) {
super();
this.id = id;
this.titolo = titolo;
}
- public long getId() {
- return id;
- }
public void setId(long id) {
this.id = id;
View
1 iti-note-model-jdbc-dao/src/iti/note/test/model/TaccuinoTestCase.java
@@ -61,6 +61,7 @@ public void test() {
}
assertFalse(tt.isEmpty());
+
try {
PostgreSQLConnection.getConn().rollback(sp);
View
BIN iti-note-model/lib/mysql-connector-java-5.1.29-bin.jar
Binary file not shown.
View
20 iti-note-model/src/META-INF/persistence.xml
@@ -8,11 +8,21 @@
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.show_sql" value="true" />
- <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
- <property name="hibernate.connection.username" value="postgres" />
- <property name="hibernate.connection.password" value="postgres" />
- <property name="hibernate.connection.url" value="jdbc:postgresql://127.0.0.1:5432/iti_note" />
- <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
+<!-- <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" /> -->
+ <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
+
+<!-- <property name="hibernate.connection.username" value="postgres" /> -->
+<property name="hibernate.connection.username" value="root" />
+
+<!-- <property name="hibernate.connection.password" value="postgres" /> -->
+<property name="hibernate.connection.password" value="toor" />
+
+<!-- <property name="hibernate.connection.url" value="jdbc:postgresql://127.0.0.1:5432/iti_note" /> -->
+ <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/iti_note" />
+
+<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" /> -->
+ <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
+
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="1800" />
View
2 iti-note-model/src/iti/note/model/Taccuino.java
@@ -31,7 +31,7 @@
private String titolo;
- @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
+ @OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "taccuino_id")
private List<Nota> note = new ArrayList<Nota>();
View
57 iti-note-model/src/iti/note/model/User.java
@@ -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;
+ }
+}
View
42 iti-note-model/src/iti/note/test/UserPersistenceTest.java
@@ -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();
+ }
+}

0 comments on commit e510ade

Please sign in to comment.