Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Uebung02_Jpa.pdf
Binary file not shown.
Binary file added Uebung03_Jpa.pdf
Binary file not shown.
Binary file added Uebung04_Jpa.pdf
Binary file not shown.
5 changes: 5 additions & 0 deletions movierental.jpa/.project
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
boot.validation.initialized=true
eclipse.preferences.version=1
3 changes: 2 additions & 1 deletion movierental.jpa/bin/main/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ spring.h2.console.path=/h2

spring.datasource.url=jdbc:h2:mem:testdb

spring.jpa.show-sql=true

# spring.datasource.url=
# spring.datasource.driver-class-name=
# spring.datasource.username=sa
# spring.datasource.password=

# logging.level.org.hibernate.SQL: DEBUG
# spring.jpa.hibernate.ddl-auto=create
# spring.jpa.show-sql=true
Binary file modified movierental.jpa/bin/main/ch/fhnw/eaf/rental/model/Movie.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified movierental.jpa/bin/main/ch/fhnw/eaf/rental/model/Rental.class
Binary file not shown.
Binary file modified movierental.jpa/bin/main/ch/fhnw/eaf/rental/model/User.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions movierental.jpa/bin/main/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ INSERT INTO USERS (USER_ID, USER_NAME, USER_FIRSTNAME, USER_EMAIL) VALUES (1, 'K
INSERT INTO USERS (USER_ID, USER_NAME, USER_FIRSTNAME, USER_EMAIL) VALUES (2, 'Knecht', 'Werner', 'werner.knecht@gmail.com');
INSERT INTO USERS (USER_ID, USER_NAME, USER_FIRSTNAME, USER_EMAIL) VALUES (3, 'Meyer', 'Barbara', 'barbara.meyer@gmail.com');
INSERT INTO USERS (USER_ID, USER_NAME, USER_FIRSTNAME, USER_EMAIL) VALUES (4, 'Kummer', 'Adolf', 'adolf.kummer@gmail.com');
INSERT INTO USERS (USER_ID, USER_NAME, USER_FIRSTNAME, USER_EMAIL) VALUES (5, 'Naef', 'Livio', 'livio.naef@students.fhnw.ch');

INSERT INTO RENTALS (RENTAL_ID, MOVIE_ID, USER_ID, RENTAL_RENTALDATE, RENTAL_RENTALDAYS) VALUES (1, 1, 1, '2019-09-28', 14);
INSERT INTO RENTALS (RENTAL_ID, MOVIE_ID, USER_ID, RENTAL_RENTALDATE, RENTAL_RENTALDAYS) VALUES (2, 2, 1, '2019-09-30', 14);
Expand Down
Binary file not shown.
26 changes: 26 additions & 0 deletions movierental.jpa/src/main/java/ch/fhnw/eaf/rental/model/Movie.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,40 @@

import java.time.LocalDate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "MOVIES")
public class Movie {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="MOVIE_ID")
private Long id;

@Column(name="MOVIE_TITLE")
private String title;

@Column(name="MOVIE_RELEASEDATE")
private LocalDate releaseDate;

@Column(name="MOVIE_RENTED")
private boolean rented;

@ManyToOne // Movie is the owner of the relationship
@JoinColumn(name="PRICECATEGORY_FK")
private PriceCategory priceCategory;

// nicht-privater no-arg Konstruktor (jede @Entity Klasse braucht diesen)
protected Movie() { }

public Movie(Long id, String title, LocalDate releaseDate, boolean rented, PriceCategory priceCategory) {
this(title, releaseDate, priceCategory);
setId(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package ch.fhnw.eaf.rental.model;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

Expand All @@ -8,8 +16,18 @@

@JsonDeserialize(using = PriceCategoryDeserializer.class)
@JsonSerialize(using = PriceCategorySerializer.class)
@Entity
@Table(name = "PRICECATEGORIES")
@DiscriminatorColumn(name="PRICECATEGORY_TYPE") // default DTYPE
public abstract class PriceCategory {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="PRICECATEGORY_ID")
private Long id;

// nicht-privater no-arg Konstruktor
protected PriceCategory() { }

public Long getId() {
return id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package ch.fhnw.eaf.rental.model;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue(value="Children")
public class PriceCategoryChildren extends PriceCategory {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package ch.fhnw.eaf.rental.model;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue(value="NewRelease")
public class PriceCategoryNewRelease extends PriceCategory {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package ch.fhnw.eaf.rental.model;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue(value="Regular")
public class PriceCategoryRegular extends PriceCategory {

@Override
Expand Down
26 changes: 26 additions & 0 deletions movierental.jpa/src/main/java/ch/fhnw/eaf/rental/model/Rental.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,44 @@

import java.time.LocalDate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Rental.class)
@Entity
@Table(name = "RENTALS")
public class Rental {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="RENTAL_ID")
private Long id;

@OneToOne
@JoinColumn(name="MOVIE_ID")
private Movie movie;

@ManyToOne // Rental is the owner of the relationship
@JoinColumn(name="USER_ID")
private User user;

@Column(name="RENTAL_RENTALDATE")
private LocalDate rentalDate;

@Column(name="RENTAL_RENTALDAYS")
private int rentalDays;

// nicht-privater no-arg Konstruktor
public Rental() {
this.rentalDate = LocalDate.now();
}
Expand Down
24 changes: 24 additions & 0 deletions movierental.jpa/src/main/java/ch/fhnw/eaf/rental/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,41 @@
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Rental.class)
@Entity
@Table(name = "USERS")
public class User {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="USER_ID")
private Long id;

@Column(name="USER_NAME")
private String lastName;

@Column(name="USER_FIRSTNAME")
private String firstName;

@Column(name="USER_EMAIL")
private String email;

@OneToMany(mappedBy="user", cascade = CascadeType.REMOVE)
private List<Rental> rentals;

// nicht-privater no-arg Konstruktor
protected User() { }

public User(String lastName, String firstName) {
this.lastName = lastName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,69 @@
import java.util.List;
import java.util.Optional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import ch.fhnw.eaf.rental.model.Movie;
import ch.fhnw.eaf.rental.persistence.MovieRepository;

@Repository
public class JpaMovieRepository implements MovieRepository {

@PersistenceContext
private EntityManager em;

@Override
public Optional<Movie> findById(Long id) {
// TODO Auto-generated method stub
return null;
return Optional.ofNullable(em.find(Movie.class, id));
}

@Override
public List<Movie> findAll() {
// TODO Auto-generated method stub
return null;
TypedQuery<Movie> query = em.createQuery("SELECT m FROM Movie m", Movie.class);
return query.getResultList();
}

@Override
public Movie save(Movie t) {
// TODO Auto-generated method stub
return null;
public Movie save(Movie movie) {
return em.merge(movie);
}

@Override
public void deleteById(Long id) {
// TODO Auto-generated method stub

em.remove(em.getReference(Movie.class, id));
}

@Override
public void delete(Movie entity) {
// TODO Auto-generated method stub

public void delete(Movie movie) {
em.remove(em.merge(movie));
}

@Override
public boolean existsById(Long id) {
// TODO Auto-generated method stub
return false;
// return findById(id).isPresent(); -> Entität wird in Persistenzkontext geladen!
TypedQuery<Long> query = em.createQuery(
"SELECT COUNT(m) FROM Movie m WHERE m.id = :id",
Long.class);
query.setParameter("id", id);
return query.getSingleResult() > 0; // -> Entität wird nicht in Persistenzkontext geladen!
}

@Override
public long count() {
// TODO Auto-generated method stub
return 0;
return em.createQuery("SELECT COUNT(m) FROM Movie m", Long.class).getSingleResult();
}

@Override
public List<Movie> findByTitle(String title) {
// TODO Auto-generated method stub
return null;
TypedQuery<Movie> query = em.createQuery(
"SELECT m FROM Movie m WHERE m.title = :title",
Movie.class);
query.setParameter("title", title);
return query.getResultList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,60 @@
import java.util.List;
import java.util.Optional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import ch.fhnw.eaf.rental.model.PriceCategory;
import ch.fhnw.eaf.rental.persistence.PriceCategoryRepository;

@Repository
public class JpaPriceCategoryRepository implements PriceCategoryRepository {

@PersistenceContext
private EntityManager em;

@Override
public Optional<PriceCategory> findById(Long id) {
// TODO Auto-generated method stub
return null;
return Optional.ofNullable(em.find(PriceCategory.class, id));
}

@Override
public List<PriceCategory> findAll() {
// TODO Auto-generated method stub
return null;
TypedQuery<PriceCategory> query = em.createQuery("SELECT pc FROM PriceCategory pc", PriceCategory.class);
return query.getResultList();
Comment thread
livionaef marked this conversation as resolved.
}

@Override
public PriceCategory save(PriceCategory t) {
// TODO Auto-generated method stub
return null;
public PriceCategory save(PriceCategory priceCategory) {
return em.merge(priceCategory);
}

@Override
public void deleteById(Long id) {
// TODO Auto-generated method stub

em.remove(em.getReference(PriceCategory.class, id));
}

@Override
public void delete(PriceCategory entity) {
// TODO Auto-generated method stub

public void delete(PriceCategory priceCategory) {
em.remove(em.merge(priceCategory));
}

@Override
public boolean existsById(Long id) {
// TODO Auto-generated method stub
return false;
// return findById(id).isPresent();
TypedQuery<Long> query = em.createQuery(
"SELECT COUNT(pc) FROM PriceCategory pc WHERE pc.id = :id",
Long.class);
query.setParameter("id", id);
return query.getSingleResult() > 0;
}

@Override
public long count() {
// TODO Auto-generated method stub
return 0;
return em.createQuery("SELECT COUNT(pc) FROM PriceCategory pc", Long.class).getSingleResult();
}

}
Loading