Created
October 19, 2018 22:41
-
-
Save yogonza524/9eb757f4ad28e82b69b373527ca3a720 to your computer and use it in GitHub Desktop.
See the video -> https://youtu.be/VSFe8CiPrVs
This file contains hidden or 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
spring.jpa.hibernate.ddl-auto=update | |
spring.datasource.driverClassName=org.h2.Driver | |
spring.datasource.url=jdbc:h2:mem:fletz;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=2;INIT=CREATE SCHEMA IF NOT EXISTS xxx\\;SET SCHEMA xxx | |
spring.datasource.username=sa | |
spring.datasource.password= | |
#This line is for Lazy load from list joined by foreign key | |
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true |
This file contains hidden or 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
INSERT INTO propietario(nombre,apellido,email,domicilio) VALUES('Gonzalo','Mendoza','[email protected]','Las Condes 725'); | |
INSERT INTO inmueble(id_propietario,titulo, longitud, latitud,domicilio) VALUES(1,'Casa Grande + Baño',27.2547,58.62587,'Barrio Siempre Viva la Patria 2345'); | |
INSERT INTO inmueble(id_propietario,titulo, longitud, latitud,domicilio) VALUES(1,'Depto 3Dormitorios + Living + NO mascotas',27.5877,57.9214,'Barrio Escondido 21'); | |
INSERT INTO inmueble(id_propietario,titulo, longitud, latitud,domicilio) VALUES(1,'Casa rodante, excelente estado',27.1322,58.4678,'Peru 1136'); |
This file contains hidden or 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
package com.relations.model; | |
import java.io.Serializable; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
/** | |
* | |
* @author gonzalo | |
*/ | |
@Entity(name = "Inmueble") | |
@Table(name = "inmueble") | |
public class Inmueble implements Serializable { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long id; | |
@Column(name = "id_propietario") | |
private Long idPropietario; | |
@Column(name = "titulo") | |
private String titulo; | |
@Column(name = "longitud") | |
private Double longitud; | |
@Column(name = "latitud") | |
private Double latitud; | |
@Column(name = "domicilio") | |
private String domicilio; | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public Long getIdPropietario() { | |
return idPropietario; | |
} | |
public void setIdPropietario(Long idPropietario) { | |
this.idPropietario = idPropietario; | |
} | |
public String getTitulo() { | |
return titulo; | |
} | |
public void setTitulo(String titulo) { | |
this.titulo = titulo; | |
} | |
public Double getLongitud() { | |
return longitud; | |
} | |
public void setLongitud(Double longitud) { | |
this.longitud = longitud; | |
} | |
public Double getLatitud() { | |
return latitud; | |
} | |
public void setLatitud(Double latitud) { | |
this.latitud = latitud; | |
} | |
public String getDomicilio() { | |
return domicilio; | |
} | |
public void setDomicilio(String domicilio) { | |
this.domicilio = domicilio; | |
} | |
@Override | |
public String toString() { | |
return "Inmueble{" + "id=" + id + ", idPropietario=" + idPropietario + ", titulo=" + titulo + ", longitud=" + longitud + ", latitud=" + latitud + ", domicilio=" + domicilio + '}'; | |
} | |
} |
This file contains hidden or 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
package com.relations.jpa; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.boot.autoconfigure.domain.EntityScan; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | |
@SpringBootApplication | |
@ComponentScan(basePackages = { | |
"com.relations" | |
}) | |
@EnableJpaRepositories("com.relations.repository") | |
@EntityScan(basePackages = {"com.relations.model"}) | |
public class JpaApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(JpaApplication.class, args); | |
} | |
} |
This file contains hidden or 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
package com.relations.jpa; | |
import com.relations.repository.PropietarioRepository; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.test.context.junit4.SpringRunner; | |
@RunWith(SpringRunner.class) | |
@SpringBootTest | |
public class JpaApplicationTests { | |
private Long idPropietario; | |
@Autowired | |
private PropietarioRepository propietarioRepository; | |
@Before | |
public void init() { | |
this.idPropietario = 1L; | |
} | |
@Test | |
public void contextLoads() { | |
} | |
@Test | |
public void findOnePropietarioTest() { | |
System.out.println("Propietario: " + propietarioRepository.findOne(idPropietario)); | |
} | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.relations</groupId> | |
<artifactId>jpa</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>jpa</name> | |
<description>Demo project for Spring Boot</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.5.17.RELEASE</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-jpa</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>com.h2database</groupId> | |
<artifactId>h2</artifactId> | |
<scope>runtime</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or 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
package com.relations.model; | |
import java.io.Serializable; | |
import java.util.List; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
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; | |
/** | |
* | |
* @author gonzalo | |
*/ | |
@Table(name = "propietario") | |
@Entity(name = "Propietario") | |
public class Propietario implements Serializable { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long id; | |
@Column(name = "nombre") | |
private String nombre; | |
@Column(name = "apellido") | |
private String apellido; | |
@Column(name = "email") | |
private String email; | |
@Column(name = "domicilio") | |
private String domicilio; | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
public String getApellido() { | |
return apellido; | |
} | |
public void setApellido(String apellido) { | |
this.apellido = apellido; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public String getDomicilio() { | |
return domicilio; | |
} | |
public void setDomicilio(String domicilio) { | |
this.domicilio = domicilio; | |
} | |
//Unidirectional | |
@OneToMany(fetch = FetchType.LAZY) | |
@JoinColumn(name = "id_propietario") | |
private List<Inmueble> inmuebles; | |
public List<Inmueble> getInmuebles() { | |
return inmuebles; | |
} | |
public void setInmuebles(List<Inmueble> inmuebles) { | |
this.inmuebles = inmuebles; | |
} | |
@Override | |
public String toString() { | |
return "Propietario{" + "id=" + id + ", nombre=" + nombre + ", apellido=" + apellido + ", email=" + email + ", domicilio=" + domicilio + ", inmuebles=" + inmuebles + '}'; | |
} | |
} |
This file contains hidden or 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
package com.relations.repository; | |
import com.relations.model.Propietario; | |
import org.springframework.data.repository.CrudRepository; | |
import org.springframework.stereotype.Repository; | |
/** | |
* | |
* @author gonzalo | |
*/ | |
@Repository | |
public interface PropietarioRepository extends CrudRepository<Propietario,Long>{ | |
} |
This file contains hidden or 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
DROP TABLE IF EXISTS propietario; | |
DROP TABLE IF EXISTS inmueble; | |
CREATE TABLE propietario( | |
id int(10) PRIMARY KEY AUTO_INCREMENT, | |
nombre varchar(256) not null, | |
apellido varchar(256) not null, | |
email varchar(256) not null, | |
domicilio varchar(256) not null | |
); | |
CREATE TABLE inmueble( | |
id int(10) PRIMARY KEY AUTO_INCREMENT, | |
id_propietario int(10) not null, | |
titulo varchar(256) not null, | |
longitud real, | |
latitud real, | |
domicilio varchar(256) not null, | |
CONSTRAINT inm_prop_fk FOREIGN KEY (id_propietario) REFERENCES propietario (id) | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment