From b080aa8369f5fc1929d3ce92de5612a67eb109b4 Mon Sep 17 00:00:00 2001
From: JotaJota96 <jjap96@gmail.com>
Date: Tue, 17 Sep 2019 15:52:16 -0300
Subject: [PATCH] Solucionado, quitadas columnas con id para Maps

---
 .../src/Logica/Clases/Administrador.java      |  2 +-
 .../src/Logica/Clases/Canal.java              |  7 ++--
 .../src/Logica/Clases/Comentario.java         |  6 ++--
 .../Logica/Clases/ListaDeReproduccion.java    | 25 ++++++-------
 .../src/Logica/Clases/Persona.java            |  2 +-
 .../src/Logica/Clases/Usuario.java            | 21 ++++++-----
 .../src/Logica/Clases/Valoracion.java         |  2 +-
 .../src/Logica/Clases/Video.java              | 12 +++----
 .../src/META-INF/persistence.xml              |  6 ++--
 .../src/Presentacion/Pryevas.java             | 36 +++++++++++++++++++
 10 files changed, 79 insertions(+), 40 deletions(-)
 create mode 100644 UyTube_aplicacion/src/Presentacion/Pryevas.java

diff --git a/UyTube_aplicacion/src/Logica/Clases/Administrador.java b/UyTube_aplicacion/src/Logica/Clases/Administrador.java
index ba5036f..373b88d 100644
--- a/UyTube_aplicacion/src/Logica/Clases/Administrador.java
+++ b/UyTube_aplicacion/src/Logica/Clases/Administrador.java
@@ -13,7 +13,7 @@ public class Administrador extends Persona implements Serializable{
     // No logre hacer que el id fuera auto-incremental, no se por que, probe de todo y no pude
     //@GeneratedValue(strategy=GenerationType.IDENTITY)
     //@Column(name = "id")
-    private int id;
+    private Integer id;
     
     //-------------------------------------------------------------------------
 
diff --git a/UyTube_aplicacion/src/Logica/Clases/Canal.java b/UyTube_aplicacion/src/Logica/Clases/Canal.java
index b869778..08f98a5 100644
--- a/UyTube_aplicacion/src/Logica/Clases/Canal.java
+++ b/UyTube_aplicacion/src/Logica/Clases/Canal.java
@@ -16,12 +16,11 @@ import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.EnumType;
 import javax.persistence.Enumerated;
-import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
-import javax.persistence.MapKeyColumn;
+import javax.persistence.MapKey;
 import javax.persistence.OneToMany;
 import javax.persistence.Table;
 
@@ -32,7 +31,7 @@ public class Canal implements Serializable {
     @Id
     @GeneratedValue(strategy=GenerationType.IDENTITY)
     @Column(name = "id")
-    private int id;
+    private Integer id;
     
     @Column(name = "nombre")
     private String nombre;
@@ -46,10 +45,12 @@ public class Canal implements Serializable {
     
     @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
     @JoinColumn(name = "id_canal")
+    @MapKey(name = "id")
     private Map<Integer, ListaDeReproduccion> misListas;
     
     @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
     @JoinColumn(name = "id_canal")
+    @MapKey(name = "id")
     private Map<Integer, Video> misVideos;
 
     @Column(name = "eliminado")
diff --git a/UyTube_aplicacion/src/Logica/Clases/Comentario.java b/UyTube_aplicacion/src/Logica/Clases/Comentario.java
index 69680a8..14fb014 100644
--- a/UyTube_aplicacion/src/Logica/Clases/Comentario.java
+++ b/UyTube_aplicacion/src/Logica/Clases/Comentario.java
@@ -9,7 +9,6 @@ import javax.persistence.Basic;
 import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
-import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
@@ -25,7 +24,7 @@ public class Comentario implements Serializable {
     @Id
     @GeneratedValue(strategy=GenerationType.IDENTITY)
     @Column(name = "id")
-    private int id;
+    private Integer id;
     
     @Basic
     @Column(name = "fecha")
@@ -42,8 +41,7 @@ public class Comentario implements Serializable {
     private Usuario usr;
     
     @OneToMany(cascade = CascadeType.ALL, 
-            orphanRemoval = true,
-            fetch = FetchType.LAZY)
+            orphanRemoval = true)
     @JoinColumn(name = "id_com_padre")
     private List<Comentario> misComentario;
     
diff --git a/UyTube_aplicacion/src/Logica/Clases/ListaDeReproduccion.java b/UyTube_aplicacion/src/Logica/Clases/ListaDeReproduccion.java
index 295794f..fa18a5c 100644
--- a/UyTube_aplicacion/src/Logica/Clases/ListaDeReproduccion.java
+++ b/UyTube_aplicacion/src/Logica/Clases/ListaDeReproduccion.java
@@ -13,14 +13,13 @@ import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.EnumType;
 import javax.persistence.Enumerated;
-import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.JoinTable;
-import javax.persistence.ManyToMany;
-import javax.persistence.MapKeyColumn;
+import javax.persistence.MapKey;
+import javax.persistence.OneToMany;
 import javax.persistence.Table;
 
 @Entity
@@ -30,7 +29,7 @@ public class ListaDeReproduccion implements Serializable {
     @Id
     @GeneratedValue(strategy=GenerationType.IDENTITY)
     @Column(name = "id")
-    private int id;
+    private Integer id;
     
     @Column(name = "nombre")
     private String nombre;
@@ -39,22 +38,24 @@ public class ListaDeReproduccion implements Serializable {
     @Column(name = "privacidad")
     private Privacidad privacidad;
     
+    @Column(name = "eliminado")
+    private boolean eliminado;
+    
     @Enumerated(EnumType.STRING)
     @Column(name = "tipo")
     private TipoListaDeReproduccion tipo;
     
     @Column(name = "categoria")
     private String categoria;
-    
-    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
-    @JoinTable(name = "videos_en_listas",
-            joinColumns = @JoinColumn(name = "id_lista"),
-            inverseJoinColumns = @JoinColumn(name = "id_video"))
+
+    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
+    @JoinTable(
+            name="videos_en_listas",
+            joinColumns={@JoinColumn(name="id_lista", referencedColumnName="id")},
+            inverseJoinColumns={@JoinColumn(name="id_video", referencedColumnName="id")})
+    @MapKey(name = "id")
     private Map<Integer, Video> misVideos;
     
-    @Column(name = "eliminado")
-    private boolean eliminado;
-    
     //-----------------------------------------------------------------------------
     public ListaDeReproduccion() {
     }
diff --git a/UyTube_aplicacion/src/Logica/Clases/Persona.java b/UyTube_aplicacion/src/Logica/Clases/Persona.java
index 599c6d0..f3dd8d4 100644
--- a/UyTube_aplicacion/src/Logica/Clases/Persona.java
+++ b/UyTube_aplicacion/src/Logica/Clases/Persona.java
@@ -11,7 +11,7 @@ import javax.persistence.InheritanceType;
 @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
 public abstract class Persona implements Serializable {
     @Id
-    private int id;
+    private Integer id;
     
     @Column(name = "nombre")
     private String nombre;
diff --git a/UyTube_aplicacion/src/Logica/Clases/Usuario.java b/UyTube_aplicacion/src/Logica/Clases/Usuario.java
index 6610fee..74a8fed 100644
--- a/UyTube_aplicacion/src/Logica/Clases/Usuario.java
+++ b/UyTube_aplicacion/src/Logica/Clases/Usuario.java
@@ -8,11 +8,10 @@ import javax.persistence.Basic;
 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.JoinTable;
-import javax.persistence.MapKeyColumn;
+import javax.persistence.MapKey;
 import javax.persistence.OneToMany;
 import javax.persistence.OneToOne;
 import javax.persistence.Table;
@@ -21,7 +20,7 @@ import javax.persistence.Table;
 @Table(name="usuario")
 public class Usuario extends Persona{
     @Id
-    @Column(name = "nickname")
+    @Column(name = "id")
     private String id;
     
     @Column(name = "correo")
@@ -50,15 +49,19 @@ public class Usuario extends Persona{
     private Canal MiCanal;
     
     @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
-    @JoinTable(name = "usuarios_seguidores",
-            joinColumns = @JoinColumn(name = "nick_seguido"),
-            inverseJoinColumns = @JoinColumn(name = "nick_seguidor"))
+    @JoinTable(
+            name="usuarios_seguidores",
+            joinColumns={@JoinColumn(name="nick_seguido", referencedColumnName="id")},
+            inverseJoinColumns={@JoinColumn(name="nick_seguidor", referencedColumnName="id")})
+    @MapKey(name = "id")
     private Map<String, Usuario> misSeguidores;
     
     @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
-    @JoinTable(name = "usuarios_seguidos",
-            joinColumns = @JoinColumn(name = "nick_seguidor"),
-            inverseJoinColumns = @JoinColumn(name = "nick_seguido"))
+    @JoinTable(
+            name="usuarios_seguidos",
+            joinColumns={@JoinColumn(name="nick_seguidor", referencedColumnName="id")},
+            inverseJoinColumns={@JoinColumn(name="nick_seguido", referencedColumnName="id")})
+    @MapKey(name = "id")
     private Map<String, Usuario> seguidos;
     
     //----------------------------------------------------------------------------------
diff --git a/UyTube_aplicacion/src/Logica/Clases/Valoracion.java b/UyTube_aplicacion/src/Logica/Clases/Valoracion.java
index 3177791..99591d6 100644
--- a/UyTube_aplicacion/src/Logica/Clases/Valoracion.java
+++ b/UyTube_aplicacion/src/Logica/Clases/Valoracion.java
@@ -22,7 +22,7 @@ public class Valoracion implements Serializable {
     @Id
     @GeneratedValue(strategy=GenerationType.IDENTITY)
     @Column(name = "id")
-    private int id;
+    private Integer id;
     
     @Enumerated(EnumType.STRING)
     @JoinColumn(name = "valoracion")
diff --git a/UyTube_aplicacion/src/Logica/Clases/Video.java b/UyTube_aplicacion/src/Logica/Clases/Video.java
index a1fb7ed..37696ba 100644
--- a/UyTube_aplicacion/src/Logica/Clases/Video.java
+++ b/UyTube_aplicacion/src/Logica/Clases/Video.java
@@ -17,12 +17,11 @@ import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.EnumType;
 import javax.persistence.Enumerated;
-import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
-import javax.persistence.MapKeyColumn;
+import javax.persistence.MapKey;
 import javax.persistence.OneToMany;
 import javax.persistence.Table;
 
@@ -33,7 +32,7 @@ public class Video implements Serializable {
     @Id
     @GeneratedValue(strategy=GenerationType.IDENTITY)
     @Column(name = "id")
-    private int id;
+    private Integer id;
     
     @Column(name = "nombre")
     private String nombre;
@@ -68,17 +67,18 @@ public class Video implements Serializable {
     @Column(name = "cant_dislikes")
     private int cantDisLikes = 0;
     
+    @Column(name = "eliminado")
+    private boolean eliminado;
+    
     @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
     @JoinColumn(name = "id_video")
     private List<Valoracion> valoraciones;
     
     @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
     @JoinColumn(name = "id_video")
+    @MapKey(name = "id")
     private Map<Integer, Comentario> comentarios;
     
-    @Column(name = "eliminado")
-    private boolean eliminado;
-    
     //------------------------------------------------------------------------
     public Video(){
         
diff --git a/UyTube_aplicacion/src/META-INF/persistence.xml b/UyTube_aplicacion/src/META-INF/persistence.xml
index db8d489..aa020d7 100644
--- a/UyTube_aplicacion/src/META-INF/persistence.xml
+++ b/UyTube_aplicacion/src/META-INF/persistence.xml
@@ -3,15 +3,15 @@
   <persistence-unit name="UyTubePU" transaction-type="RESOURCE_LOCAL">
     <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
     <class>Logica.Clases.Administrador</class>
-    <class>Logica.Clases.Persona</class>
-    <class>Logica.Clases.Valoracion</class>
     <class>Logica.Clases.Canal</class>
     <class>Logica.Clases.Categoria</class>
     <class>Logica.Clases.Comentario</class>
     <class>Logica.Clases.ListaDeReproduccion</class>
+    <class>Logica.Clases.ListaPorDefecto</class>
+    <class>Logica.Clases.Persona</class>
     <class>Logica.Clases.Usuario</class>
+    <class>Logica.Clases.Valoracion</class>
     <class>Logica.Clases.Video</class>
-    <class>Logica.Clases.ListaPorDefecto</class>
     <properties>
       <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/UyTube"/>
       <property name="javax.persistence.jdbc.user" value="postgres"/>
diff --git a/UyTube_aplicacion/src/Presentacion/Pryevas.java b/UyTube_aplicacion/src/Presentacion/Pryevas.java
new file mode 100644
index 0000000..e5cfd30
--- /dev/null
+++ b/UyTube_aplicacion/src/Presentacion/Pryevas.java
@@ -0,0 +1,36 @@
+package Presentacion;
+
+import Logica.Clases.Canal;
+import Logica.Clases.Usuario;
+import Logica.Clases.Video;
+import Logica.DataType.DtCanal;
+import Logica.DataType.DtComentario;
+import Logica.DataType.DtListaDeReproduccion;
+import Logica.DataType.DtUsuario;
+import Logica.Enumerados.Privacidad;
+import java.sql.Date;
+import java.sql.Time;
+import java.util.ArrayList;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+
+public class Pryevas {
+    
+    public static void main(String[] args) {
+        
+        EntityManagerFactory factory = null;
+        EntityManager manager = null;
+        
+        try {
+            factory = Persistence.createEntityManagerFactory("UyTubePU");
+            manager = factory.createEntityManager();
+            
+        } catch (Exception e) {
+            System.out.println("//////////////////////////");
+            System.out.println(e.getMessage());
+            System.out.println("//////////////////////////");
+        }
+        
+    }
+}
-- 
GitLab