Skip to content

Commit

Permalink
Finish userentity
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandro-aglietti committed Feb 21, 2014
2 parents a7028a6 + 42a86e2 commit e510ade
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 15 deletions.
13 changes: 4 additions & 9 deletions 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;
Expand All @@ -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;
Expand Down
Expand Up @@ -61,6 +61,7 @@ public void test() {
}

assertFalse(tt.isEmpty());


try {
PostgreSQLConnection.getConn().rollback(sp);
Expand Down
Binary file not shown.
20 changes: 15 additions & 5 deletions iti-note-model/src/META-INF/persistence.xml
Expand Up @@ -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" />
Expand Down
2 changes: 1 addition & 1 deletion iti-note-model/src/iti/note/model/Taccuino.java
Expand Up @@ -31,7 +31,7 @@ public class Taccuino implements Serializable {

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>();

Expand Down
57 changes: 57 additions & 0 deletions 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;
}
}
42 changes: 42 additions & 0 deletions 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.