Initial commit
This commit is contained in:
commit
894b52edb4
|
@ -0,0 +1,38 @@
|
|||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea/modules.xml
|
||||
.idea/jarRepositories.xml
|
||||
.idea/compiler.xml
|
||||
.idea/libraries/
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,67 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,javax.persistence.EntityManager,unwrap" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PyCompatibilityInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="ourVersions">
|
||||
<value>
|
||||
<list size="7">
|
||||
<item index="0" class="java.lang.String" itemvalue="3.7" />
|
||||
<item index="1" class="java.lang.String" itemvalue="3.8" />
|
||||
<item index="2" class="java.lang.String" itemvalue="3.9" />
|
||||
<item index="3" class="java.lang.String" itemvalue="3.10" />
|
||||
<item index="4" class="java.lang.String" itemvalue="3.11" />
|
||||
<item index="5" class="java.lang.String" itemvalue="3.10" />
|
||||
<item index="6" class="java.lang.String" itemvalue="3.11" />
|
||||
</list>
|
||||
</value>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="ignoredPackages">
|
||||
<value>
|
||||
<list size="24">
|
||||
<item index="0" class="java.lang.String" itemvalue="httpx" />
|
||||
<item index="1" class="java.lang.String" itemvalue="six" />
|
||||
<item index="2" class="java.lang.String" itemvalue="python-dateutil" />
|
||||
<item index="3" class="java.lang.String" itemvalue="aiofiles" />
|
||||
<item index="4" class="java.lang.String" itemvalue="h11" />
|
||||
<item index="5" class="java.lang.String" itemvalue="MarkupSafe" />
|
||||
<item index="6" class="java.lang.String" itemvalue="numpy" />
|
||||
<item index="7" class="java.lang.String" itemvalue="rfc3986" />
|
||||
<item index="8" class="java.lang.String" itemvalue="click" />
|
||||
<item index="9" class="java.lang.String" itemvalue="Jinja2" />
|
||||
<item index="10" class="java.lang.String" itemvalue="sniffio" />
|
||||
<item index="11" class="java.lang.String" itemvalue="demjson" />
|
||||
<item index="12" class="java.lang.String" itemvalue="addict" />
|
||||
<item index="13" class="java.lang.String" itemvalue="pandas" />
|
||||
<item index="14" class="java.lang.String" itemvalue="starlette" />
|
||||
<item index="15" class="java.lang.String" itemvalue="certifi" />
|
||||
<item index="16" class="java.lang.String" itemvalue="pytz" />
|
||||
<item index="17" class="java.lang.String" itemvalue="uvicorn" />
|
||||
<item index="18" class="java.lang.String" itemvalue="itsdangerous" />
|
||||
<item index="19" class="java.lang.String" itemvalue="justpy" />
|
||||
<item index="20" class="java.lang.String" itemvalue="websockets" />
|
||||
<item index="21" class="java.lang.String" itemvalue="httpcore" />
|
||||
<item index="22" class="java.lang.String" itemvalue="idna" />
|
||||
<item index="23" class="java.lang.String" itemvalue="psycopg2" />
|
||||
</list>
|
||||
</value>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="ignoredIdentifiers">
|
||||
<list>
|
||||
<option value="blog.models.Post.objects" />
|
||||
<option value="reviews.models.Review.objects" />
|
||||
<option value="django.db.models.fields.related.OneToOneField.email" />
|
||||
<option value="accounts.models.UserProfile.objects" />
|
||||
<option value="accounts.models.User.DoesNotExist" />
|
||||
<option value="vendor.models.Vendor.objects" />
|
||||
</list>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,21 @@
|
|||
# Projekt II na Pracownię Programowania
|
||||
|
||||
---
|
||||
Aplikacja używa [PostgreSQL](https://www.postgresql.org/download/)
|
||||
|
||||
## Ważne:
|
||||
1. Przed uruchomieniem programu należy w pliku `src/main/resources/META-INF/persistence.xml`
|
||||
ustawić nazwę użytkownika i hasło (oraz url - jeśli nie użyło się domyslnych wartości przy instalacji [Postgresu](https://www.postgresql.org/download/)) na własne.
|
||||
2. W `persistence.xml` pole `hibernate.hbm2ddl.auto` jest domyślnie ustawione na `create`, a więc baza będzie usuwana i tworzona na nowo przy każdym uruchomieniu aplikacji.
|
||||
Aby zmienić zachowanie bazy, należy zmienić wartość tego pola na jedno z dostępnych:
|
||||
[link](https://stackoverflow.com/questions/438146/what-are-the-possible-values-of-the-hibernate-hbm2ddl-auto-configuration-and-wha).
|
||||
3. `Main` jest jedynie prostym przykładem wykorzystania możliwości programu.
|
||||
|
||||
## Klasy/Tabele i ich własności:
|
||||
- **Book (books)** - Klasa reprezentująca tabelę o książkach. Relacja *one-to-many* z **authors_books** i *many-to-one* z **genres**.
|
||||
- **Author (authors)** - Klasa reprezentująca tabelę o autorach książek. Relacja *one-to-many* z **authors_books**.
|
||||
- **AuthorBook (authors_books)** - Klasa reprezentująca tabelę łączącą dla tabel **books** i **authors** (potrzebne do relacji *many-to-many*).
|
||||
- **Bestseller (bestsellers)** - Klasa reprezentująca tabelę o książkach-bestsellerach. Relacja *one-to-one* z **books**.
|
||||
- **Genre (genres)** - Klasa reprezentująca tabelę o gatunkach książek.
|
||||
|
||||
*Wszystkie klasy mają metody dla podstawowych operacji CRUD*.
|
|
@ -0,0 +1,73 @@
|
|||
<?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>org.example</groupId>
|
||||
<artifactId>HibernateProject</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
<version>5.4.33.Final</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>5.4.33.Final</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>6.2.5.Final</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate.common</groupId>
|
||||
<artifactId>hibernate-commons-annotations</artifactId>
|
||||
<version>4.0.2.Final</version>
|
||||
<classifier>tests</classifier>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate.javax.persistence</groupId>
|
||||
<artifactId>hibernate-jpa-2.0-api</artifactId>
|
||||
<version>1.0.1.Final</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-impl</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.2.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.5.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,124 @@
|
|||
package org.mikgul;
|
||||
|
||||
import org.hibernate.Session;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.query.Query;
|
||||
|
||||
@Entity
|
||||
@Table(name = "authors")
|
||||
public class Author {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "first_name", nullable = false)
|
||||
private String firstName;
|
||||
|
||||
@Column(name = "last_name", nullable = false)
|
||||
private String lastName;
|
||||
|
||||
@Column(name = "date_of_birth")
|
||||
private LocalDate dateOfBirth;
|
||||
|
||||
private Boolean alive;
|
||||
|
||||
@OneToMany(mappedBy = "author")
|
||||
private List<AuthorBook> authorBooks;
|
||||
|
||||
public Author(String firstName, String lastName, LocalDate dateOfBirth, boolean alive) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.dateOfBirth = dateOfBirth;
|
||||
this.alive = alive;
|
||||
this.authorBooks = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Author(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.dateOfBirth = null;
|
||||
this.alive = null;
|
||||
this.authorBooks = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Author() {}
|
||||
|
||||
public List<AuthorBook> getAuthorBooks() {
|
||||
return authorBooks;
|
||||
}
|
||||
|
||||
public void addAuthorBook(AuthorBook authorBook) { this.authorBooks.add(authorBook); }
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public LocalDate getDateOfBirth() {
|
||||
return dateOfBirth;
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
return alive;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public void setDateOfBirth(LocalDate dateOfBirth) {
|
||||
this.dateOfBirth = dateOfBirth;
|
||||
}
|
||||
|
||||
public void setAlive(boolean alive) {
|
||||
this.alive = alive;
|
||||
}
|
||||
|
||||
public void setAuthorBooks(List<AuthorBook> authorBooks) {
|
||||
this.authorBooks = authorBooks;
|
||||
}
|
||||
|
||||
public void save(Session session) {
|
||||
session.beginTransaction();
|
||||
session.save(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public static Author getById(Session session, Long authorId) {
|
||||
return session.get(Author.class, authorId);
|
||||
}
|
||||
|
||||
public void update(Session session) {
|
||||
session.beginTransaction();
|
||||
session.update(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public void delete(Session session) {
|
||||
session.beginTransaction();
|
||||
session.delete(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public static List<Author> getAuthorsByLastName(Session session, String lastName) {
|
||||
Query<Author> query = session.createQuery("FROM Author WHERE last_name = :lastName", Author.class);
|
||||
query.setParameter("lastName", lastName);
|
||||
return query.getResultList();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package org.mikgul;
|
||||
|
||||
import org.hibernate.Session;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "authors_books")
|
||||
public class AuthorBook {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "author_id", referencedColumnName = "id")
|
||||
private Author author;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "book_id", referencedColumnName = "id")
|
||||
private Book book;
|
||||
|
||||
public AuthorBook(Author author, Book book) {
|
||||
this.author = author;
|
||||
this.book = book;
|
||||
author.addAuthorBook(this);
|
||||
book.addAuthorBook(this);
|
||||
}
|
||||
|
||||
public AuthorBook() {}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Author getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public Book getBook() {
|
||||
return book;
|
||||
}
|
||||
|
||||
public void setAuthor(Author author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public void setBook(Book book) {
|
||||
this.book = book;
|
||||
}
|
||||
|
||||
public void save(Session session) {
|
||||
session.beginTransaction();
|
||||
session.save(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public static AuthorBook getById(Session session, Long authorBookId) {
|
||||
return session.get(AuthorBook.class, authorBookId);
|
||||
}
|
||||
|
||||
public void update(Session session) {
|
||||
session.beginTransaction();
|
||||
session.update(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public void delete(Session session) {
|
||||
session.beginTransaction();
|
||||
session.delete(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,136 @@
|
|||
package org.mikgul;
|
||||
|
||||
import org.hibernate.Session;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Bestseller {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@OneToOne(cascade = CascadeType.PERSIST)
|
||||
@JoinColumn(name = "book_id", referencedColumnName = "id")
|
||||
private Book book;
|
||||
|
||||
@Column(name = "copies_sold", nullable = false)
|
||||
private long copiesSold;
|
||||
|
||||
@Column(name = "rating", nullable = false)
|
||||
private short rating;
|
||||
|
||||
@Column(name = "movie_adaptation")
|
||||
private Boolean movieAdaptation;
|
||||
|
||||
@Column(name = "release_date")
|
||||
private ZonedDateTime releaseDate;
|
||||
|
||||
private Integer awards;
|
||||
|
||||
public Bestseller(Book book, long copiesSold, short rating, Boolean movieAdaptation, ZonedDateTime releaseDate, Integer awards) {
|
||||
this.book = book;
|
||||
this.copiesSold = copiesSold;
|
||||
this.rating = rating;
|
||||
this.movieAdaptation = movieAdaptation;
|
||||
this.releaseDate = releaseDate;
|
||||
this.awards = awards;
|
||||
}
|
||||
|
||||
public Bestseller(Book book, long copiesSold, short rating) {
|
||||
this.book = book;
|
||||
this.copiesSold = copiesSold;
|
||||
this.rating = rating;
|
||||
this.movieAdaptation = null;
|
||||
this.releaseDate = null;
|
||||
this.awards = null;
|
||||
}
|
||||
|
||||
public Bestseller() {}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public ZonedDateTime getReleaseDate() {
|
||||
return releaseDate;
|
||||
}
|
||||
|
||||
public Book getBook() {
|
||||
return book;
|
||||
}
|
||||
|
||||
public long getCopiesSold() {
|
||||
return copiesSold;
|
||||
}
|
||||
|
||||
public int getRating() {
|
||||
return rating;
|
||||
}
|
||||
|
||||
public Boolean hasMovieAdaptation() {
|
||||
return movieAdaptation;
|
||||
}
|
||||
|
||||
public Integer getAwards() {
|
||||
return awards;
|
||||
}
|
||||
|
||||
public void setBook(Book book) {
|
||||
this.book = book;
|
||||
}
|
||||
|
||||
public void setCopiesSold(long copiesSold) {
|
||||
this.copiesSold = copiesSold;
|
||||
}
|
||||
|
||||
public void setRating(short rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public void setMovieAdaptation(boolean movieAdaptation) {
|
||||
this.movieAdaptation = movieAdaptation;
|
||||
}
|
||||
|
||||
public void setReleaseDate(ZonedDateTime releaseDate) {
|
||||
this.releaseDate = releaseDate;
|
||||
}
|
||||
|
||||
public void setAwards(Integer awards) {
|
||||
this.awards = awards;
|
||||
}
|
||||
|
||||
public void save(Session session) {
|
||||
session.beginTransaction();
|
||||
session.save(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public static Bestseller getById(Session session, Long bestsellerId) {
|
||||
return session.get(Bestseller.class, bestsellerId);
|
||||
}
|
||||
|
||||
public void update(Session session) {
|
||||
session.beginTransaction();
|
||||
session.update(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public void delete(Session session) {
|
||||
session.beginTransaction();
|
||||
session.delete(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public static List<Bestseller> getBestsellersWithHighRatings(Session session, short minRating) {
|
||||
if (minRating < 0 || minRating > 100) {
|
||||
System.err.println("Rating must be a number between 0-100.");
|
||||
return null;
|
||||
}
|
||||
return session.createQuery("FROM Bestseller b WHERE b.rating >= :minRating", Bestseller.class)
|
||||
.setParameter("minRating", minRating)
|
||||
.getResultList();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
package org.mikgul;
|
||||
|
||||
import org.hibernate.Session;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "books")
|
||||
public class Book {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String title;
|
||||
|
||||
private boolean bestseller;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "genre_id", referencedColumnName = "id")
|
||||
private Genre genre;
|
||||
|
||||
@OneToMany(mappedBy = "book")
|
||||
private List<AuthorBook> authorBooks;
|
||||
|
||||
public List<AuthorBook> getAuthorBooks() {
|
||||
return authorBooks;
|
||||
}
|
||||
|
||||
public Book(String title, boolean bestseller, Genre genre) {
|
||||
this.title = title;
|
||||
this.bestseller = bestseller;
|
||||
this.genre = genre;
|
||||
this.authorBooks = new ArrayList<AuthorBook>();
|
||||
}
|
||||
|
||||
public Book() {}
|
||||
|
||||
public void addAuthorBook(AuthorBook authorBook) { this.authorBooks.add(authorBook); }
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public boolean isBestseller() {
|
||||
return bestseller;
|
||||
}
|
||||
|
||||
public Genre getGenre() {
|
||||
return genre;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public void setBestseller(boolean bestseller) {
|
||||
this.bestseller = bestseller;
|
||||
}
|
||||
|
||||
public void setGenre(Genre genre) {
|
||||
this.genre = genre;
|
||||
}
|
||||
|
||||
public void setAuthorBooks(List<AuthorBook> authorBooks) {
|
||||
this.authorBooks = authorBooks;
|
||||
}
|
||||
|
||||
public void save(Session session) {
|
||||
session.beginTransaction();
|
||||
session.save(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public static Book getById(Session session, Long bookId) {
|
||||
return session.get(Book.class, bookId);
|
||||
}
|
||||
|
||||
public void update(Session session) {
|
||||
session.beginTransaction();
|
||||
session.update(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public void delete(Session session) {
|
||||
session.beginTransaction();
|
||||
session.delete(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public static List<Book> getBooksPaginated(Session session, int pageNumber, int pageSize) {
|
||||
if (pageNumber < 0 || pageSize < 0) {
|
||||
System.err.println("Page number and page size cannot be negative.");
|
||||
return null;
|
||||
}
|
||||
return session.createQuery("FROM Book", Book.class)
|
||||
.setFirstResult((pageNumber - 1) * pageSize)
|
||||
.setMaxResults(pageSize)
|
||||
.getResultList();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package org.mikgul;
|
||||
|
||||
import org.hibernate.Session;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "genres")
|
||||
public class Genre {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
public Genre(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Genre() {}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void save(Session session) {
|
||||
session.beginTransaction();
|
||||
session.save(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public static Genre getById(Session session, Long genreId) {
|
||||
return session.get(Genre.class, genreId);
|
||||
}
|
||||
|
||||
public void update(Session session) {
|
||||
session.beginTransaction();
|
||||
session.update(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
public void delete(Session session) {
|
||||
session.beginTransaction();
|
||||
session.delete(this);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,111 @@
|
|||
package org.mikgul;
|
||||
|
||||
import org.hibernate.Session;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// przykladowe dane
|
||||
Author author1 = new Author("John", "Doe", LocalDate.of(1962, 2, 1), true);
|
||||
Author author2 = new Author("Mary", "Sue", LocalDate.of(1954, 5, 21), false);
|
||||
Author author3 = new Author("Joe", "Doe", LocalDate.of(1828, 11, 29), false);
|
||||
|
||||
Genre genre1 = new Genre("Fantasy");
|
||||
Genre genre2 = new Genre("Historical");
|
||||
|
||||
Book book1 = new Book("Famous book", true, genre1);
|
||||
Book book2 = new Book("Not-so-famous book", false, genre2);
|
||||
Book book3 = new Book("Very famous book", true, genre2);
|
||||
Book book4 = new Book("Old and famous book", true, genre1);
|
||||
|
||||
Bestseller bestseller1 = new Bestseller(book1, 92147823L, (short) 72);
|
||||
Bestseller bestseller2 = new Bestseller(book4, 1230589251, (short) 90);
|
||||
Bestseller bestseller3 = new Bestseller(book3,
|
||||
105134113L,
|
||||
(short) 79,
|
||||
true,
|
||||
ZonedDateTime.of(1990, 4, 5, 9, 0, 0, 0, ZoneId.of("+02:00")),
|
||||
4);
|
||||
|
||||
AuthorBook authorBook1 = new AuthorBook(author1, book1);
|
||||
AuthorBook authorBook2 = new AuthorBook(author2, book1);
|
||||
AuthorBook authorBook3 = new AuthorBook(author2, book2);
|
||||
AuthorBook authorBook4 = new AuthorBook(author1, book3);
|
||||
AuthorBook authorBook5 = new AuthorBook(author3, book4);
|
||||
|
||||
// Zapytania (Queries)
|
||||
try {
|
||||
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-dynamic");
|
||||
|
||||
EntityManager entityManager = entityManagerFactory.createEntityManager();
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
|
||||
// zapisuje przykladowe obiekty do bazy
|
||||
author1.save(session);
|
||||
author2.save(session);
|
||||
author3.save(session);
|
||||
genre1.save(session);
|
||||
genre2.save(session);
|
||||
book1.save(session);
|
||||
book2.save(session);
|
||||
book3.save(session);
|
||||
book4.save(session);
|
||||
bestseller1.save(session);
|
||||
bestseller2.save(session);
|
||||
bestseller3.save(session);
|
||||
authorBook1.save(session);
|
||||
authorBook2.save(session);
|
||||
authorBook3.save(session);
|
||||
authorBook4.save(session);
|
||||
authorBook5.save(session);
|
||||
|
||||
// pokazuje bestsellery z ocena przynajmniej tak wysoka jak podana
|
||||
System.out.println("===Informacje o bestsellerach z oceną przynajmniej 75/100===");
|
||||
List<Bestseller> bestsellers = Bestseller.getBestsellersWithHighRatings(session, (short) 75);
|
||||
if (bestsellers != null) {
|
||||
for (Bestseller b : bestsellers) {
|
||||
System.out.printf("Title: %s\nRelease date: %s\nRating: %d/100\nAwards: %d\n\n",
|
||||
b.getBook().getTitle(),
|
||||
b.getReleaseDate(),
|
||||
b.getRating(),
|
||||
b.getAwards());
|
||||
}
|
||||
}
|
||||
|
||||
// pokazuje informacje o autorach z podanym nazwiskiem
|
||||
System.out.println("===Informacje o autorach z nazwiskiem `Doe`===");
|
||||
for (Author a : Author.getAuthorsByLastName(session, "Doe")) {
|
||||
System.out.printf("First name: %s\nDate of birth: %s\nAlive: %b\nBooks written: \n",
|
||||
a.getFirstName(), a.getDateOfBirth(), a.isAlive());
|
||||
for (AuthorBook ab : a.getAuthorBooks()) {
|
||||
System.out.printf(" - %s\n", ab.getBook().getTitle());
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
// pokazuje ksiazki (metoda stronicowana)
|
||||
System.out.println("===Lista książek (stronicowane)===");
|
||||
List<Book> books = Book.getBooksPaginated(session, 1, 3);
|
||||
if (books != null) {
|
||||
for (Book b : books) {
|
||||
System.out.printf("\nTitle: %s\nGenre: %s\nIs bestseller: %b\nAuthors:\n",
|
||||
b.getTitle(), b.getGenre().getName(), b.isBestseller());
|
||||
for (AuthorBook ab : b.getAuthorBooks()) {
|
||||
System.out.printf(" - %s %s\n", ab.getAuthor().getFirstName(), ab.getAuthor().getLastName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
session.close();
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("Creating session failed." + ex);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
||||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
|
||||
<persistence-unit name="hibernate-dynamic" transaction-type="RESOURCE_LOCAL">
|
||||
<properties>
|
||||
<!-- Configuring JDBC properties -->
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres"/>
|
||||
<property name="javax.persistence.jdbc.user" value="postgres"/>
|
||||
<property name="javax.persistence.jdbc.password" value="postgres"/>
|
||||
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
|
||||
|
||||
<!-- Hibernate properties -->
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
|
||||
<property name="hibernate.hbm2ddl.auto" value="create"/>
|
||||
<property name="hibernate.format_sql" value="false"/>
|
||||
<property name="hibernate.show_sql" value="false"/>
|
||||
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
|
@ -0,0 +1,127 @@
|
|||
import org.hibernate.Session;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.mikgul.Author;
|
||||
import org.mikgul.AuthorBook;
|
||||
import org.mikgul.Book;
|
||||
import org.mikgul.Genre;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TestAuthor {
|
||||
private Session session;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
try {
|
||||
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-dynamic");
|
||||
|
||||
EntityManager entityManager = entityManagerFactory.createEntityManager();
|
||||
this.session = entityManager.unwrap(Session.class);
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("Creating session failed." + ex);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (this.session != null) {
|
||||
this.session.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCRUD() {
|
||||
Genre genre = new Genre("Thriller");
|
||||
Book book = new Book("Test title", false, genre);
|
||||
Author author = new Author("John", "Doe", LocalDate.of(1990, 1, 1), true);
|
||||
AuthorBook authorBook = new AuthorBook(author, book);
|
||||
|
||||
// CREATE
|
||||
author.save(session);
|
||||
assertNotNull(author.getId());
|
||||
|
||||
// READ
|
||||
Author retrievedAuthor = Author.getById(session, author.getId());
|
||||
assertNotNull(retrievedAuthor);
|
||||
assertEquals(author.getFirstName(), retrievedAuthor.getFirstName());
|
||||
assertEquals(author.getLastName(), retrievedAuthor.getLastName());
|
||||
assertEquals(author.getDateOfBirth(), retrievedAuthor.getDateOfBirth());
|
||||
assertEquals(author.isAlive(), retrievedAuthor.isAlive());
|
||||
assertTrue(retrievedAuthor.getAuthorBooks().contains(authorBook));
|
||||
|
||||
// UPDATE
|
||||
retrievedAuthor.setFirstName("Jacob");
|
||||
retrievedAuthor.setLastName("Smith");
|
||||
retrievedAuthor.setAlive(false);
|
||||
retrievedAuthor.update(session);
|
||||
Author updatedAuthor = Author.getById(session, retrievedAuthor.getId());
|
||||
assertNotNull(updatedAuthor);
|
||||
assertEquals("Jacob", updatedAuthor.getFirstName());
|
||||
assertEquals("Smith", updatedAuthor.getLastName());
|
||||
assertFalse(updatedAuthor.isAlive());
|
||||
|
||||
// DELETE
|
||||
author.delete(session);
|
||||
Author deletedAuthor = Author.getById(session, author.getId());
|
||||
assertNull(deletedAuthor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAuthorsByLastName() {
|
||||
Author author1 = new Author("John", "Doe", LocalDate.of(1990, 1, 1), true);
|
||||
author1.save(session);
|
||||
Author author2 = new Author("Jacob", "Doe", LocalDate.of(1954, 12, 10), false);
|
||||
author2.save(session);
|
||||
Author author3 = new Author("Mary", "Sue", LocalDate.of(1823, 5, 13), false);
|
||||
author3.save(session);
|
||||
|
||||
List<Author> authors = new ArrayList<>();
|
||||
authors.add(author1);
|
||||
authors.add(author2);
|
||||
authors.add(author3);
|
||||
|
||||
List<Author> retrievedAuthors = Author.getAuthorsByLastName(session, "Doe");
|
||||
assertNotNull(retrievedAuthors);
|
||||
assertFalse(retrievedAuthors.isEmpty());
|
||||
for (Author ra : retrievedAuthors) {
|
||||
assertTrue(authors.contains(ra));
|
||||
}
|
||||
|
||||
author3.setLastName("Doe");
|
||||
author3.update(session);
|
||||
|
||||
retrievedAuthors = Author.getAuthorsByLastName(session, "Doe");
|
||||
assertNotNull(retrievedAuthors);
|
||||
assertEquals(retrievedAuthors, authors);
|
||||
|
||||
author1.delete(session);
|
||||
author2.delete(session);
|
||||
author3.delete(session);
|
||||
|
||||
retrievedAuthors = Author.getAuthorsByLastName(session, "Doe");
|
||||
assertNotNull(retrievedAuthors);
|
||||
assertTrue(retrievedAuthors.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAuthorsByLastNameEmpty() {
|
||||
Author author1 = new Author("John", "Doe", LocalDate.of(1990, 1, 1), true);
|
||||
author1.save(session);
|
||||
|
||||
List<Author> retrievedAuthors = Author.getAuthorsByLastName(session, "Invalid");
|
||||
assertNotNull(retrievedAuthors);
|
||||
assertTrue(retrievedAuthors.isEmpty());
|
||||
|
||||
author1.delete(session);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,137 @@
|
|||
import org.hibernate.Session;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.mikgul.*;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TestBestseller {
|
||||
private Session session;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
try {
|
||||
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-dynamic");
|
||||
|
||||
EntityManager entityManager = entityManagerFactory.createEntityManager();
|
||||
this.session = entityManager.unwrap(Session.class);
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("Creating session failed." + ex);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (this.session != null) {
|
||||
this.session.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCRUD() {
|
||||
Genre genre = new Genre("Romance");
|
||||
genre.save(session);
|
||||
Book book = new Book("Test title", true, genre);
|
||||
book.save(session);
|
||||
Bestseller bestseller = new Bestseller(book,
|
||||
105134113L,
|
||||
(short) 97,
|
||||
true,
|
||||
ZonedDateTime.of(1990, 4, 5, 9, 0, 0, 0, ZoneId.of("+02:00")),
|
||||
null);
|
||||
|
||||
// CREATE
|
||||
bestseller.save(session);
|
||||
assertNotNull(bestseller.getId());
|
||||
|
||||
// READ
|
||||
Bestseller retrievedBestseller = Bestseller.getById(session, bestseller.getId());
|
||||
assertNotNull(retrievedBestseller);
|
||||
assertEquals(book, retrievedBestseller.getBook());
|
||||
assertEquals(bestseller.getCopiesSold(), retrievedBestseller.getCopiesSold());
|
||||
assertEquals(bestseller.getRating(), retrievedBestseller.getRating());
|
||||
assertEquals(bestseller.getAwards(), retrievedBestseller.getAwards());
|
||||
|
||||
// UPDATE
|
||||
retrievedBestseller.setCopiesSold(611295875L);
|
||||
retrievedBestseller.setRating((short) 31);
|
||||
retrievedBestseller.setAwards(2);
|
||||
retrievedBestseller.update(session);
|
||||
Bestseller updatedBestseller = Bestseller.getById(session, retrievedBestseller.getId());
|
||||
assertNotNull(updatedBestseller);
|
||||
assertEquals(611295875L, updatedBestseller.getCopiesSold());
|
||||
assertEquals((short) 31, updatedBestseller.getRating());
|
||||
assertEquals((Integer) 2, updatedBestseller.getAwards());
|
||||
|
||||
// DELETE
|
||||
bestseller.delete(session);
|
||||
Bestseller deletedBestseller = Bestseller.getById(session, bestseller.getId());
|
||||
assertNull(deletedBestseller);
|
||||
|
||||
book.delete(session);
|
||||
genre.delete(session);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBestsellersWithHighRatings() {
|
||||
Genre genre = new Genre("Romance");
|
||||
genre.save(session);
|
||||
Book book1 = new Book("Test title 1", true, genre);
|
||||
book1.save(session);
|
||||
Book book2 = new Book("Test title 2", true, genre);
|
||||
book2.save(session);
|
||||
Book book3 = new Book("Test title 3", true, genre);
|
||||
book3.save(session);
|
||||
Bestseller bestseller1 = new Bestseller(book1, 105134113L, (short) 97);
|
||||
bestseller1.save(session);
|
||||
Bestseller bestseller2 = new Bestseller(book2, 412459582L, (short) 75);
|
||||
bestseller2.save(session);
|
||||
Bestseller bestseller3 = new Bestseller(book3, 9203112L, (short) 82);
|
||||
bestseller3.save(session);
|
||||
|
||||
List<Bestseller> bestsellers = new ArrayList<>();
|
||||
bestsellers.add(bestseller1);
|
||||
bestsellers.add(bestseller2);
|
||||
bestsellers.add(bestseller3);
|
||||
|
||||
List<Bestseller> retrievedBestsellers = Bestseller.getBestsellersWithHighRatings(session, (short) 80);
|
||||
assertNotNull(retrievedBestsellers);
|
||||
assertFalse(retrievedBestsellers.isEmpty());
|
||||
for (Bestseller rb : retrievedBestsellers) {
|
||||
assertTrue(bestsellers.contains(rb));
|
||||
}
|
||||
|
||||
retrievedBestsellers = Bestseller.getBestsellersWithHighRatings(session, (short) 0);
|
||||
assertNotNull(retrievedBestsellers);
|
||||
assertEquals(retrievedBestsellers, bestsellers);
|
||||
|
||||
bestseller1.delete(session);
|
||||
bestseller2.delete(session);
|
||||
bestseller3.delete(session);
|
||||
book1.delete(session);
|
||||
book2.delete(session);
|
||||
book3.delete(session);
|
||||
genre.delete(session);
|
||||
|
||||
retrievedBestsellers = Bestseller.getBestsellersWithHighRatings(session, (short) 0);
|
||||
assertNotNull(retrievedBestsellers);
|
||||
assertTrue(retrievedBestsellers.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBestsellersWithHighRatingsError() {
|
||||
assertNull(Bestseller.getBestsellersWithHighRatings(session, (short) -1));
|
||||
assertNull(Bestseller.getBestsellersWithHighRatings(session, (short) 101));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,126 @@
|
|||
import org.hibernate.Session;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.mikgul.Author;
|
||||
import org.mikgul.AuthorBook;
|
||||
import org.mikgul.Book;
|
||||
import org.mikgul.Genre;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TestBook {
|
||||
private Session session;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
try {
|
||||
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-dynamic");
|
||||
|
||||
EntityManager entityManager = entityManagerFactory.createEntityManager();
|
||||
this.session = entityManager.unwrap(Session.class);
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("Creating session failed." + ex);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (this.session != null) {
|
||||
this.session.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCRUD() {
|
||||
Genre genre = new Genre("Thriller");
|
||||
genre.save(session);
|
||||
Book book = new Book("Test title", false, genre);
|
||||
Author author = new Author("John", "Doe", LocalDate.of(1900, 1, 1), false);
|
||||
AuthorBook authorBook = new AuthorBook(author, book);
|
||||
|
||||
// CREATE
|
||||
book.save(session);
|
||||
assertNotNull(book.getId());
|
||||
|
||||
// READ
|
||||
Book retrievedBook = Book.getById(session, book.getId());
|
||||
assertNotNull(retrievedBook);
|
||||
assertEquals(book.getId(), retrievedBook.getId());
|
||||
assertEquals(book.getTitle(), retrievedBook.getTitle());
|
||||
assertEquals(book.isBestseller(), retrievedBook.isBestseller());
|
||||
assertEquals(genre, retrievedBook.getGenre());
|
||||
assertTrue(retrievedBook.getAuthorBooks().contains(authorBook));
|
||||
|
||||
// UPDATE
|
||||
retrievedBook.setTitle("Updated title");
|
||||
retrievedBook.setBestseller(true);
|
||||
retrievedBook.update(session);
|
||||
Book updatedBook = Book.getById(session, retrievedBook.getId());
|
||||
assertNotNull(updatedBook);
|
||||
assertEquals("Updated title", updatedBook.getTitle());
|
||||
assertTrue(updatedBook.isBestseller());
|
||||
|
||||
// DELETE
|
||||
book.delete(session);
|
||||
Book deletedBook = Book.getById(session, book.getId());
|
||||
assertNull(deletedBook);
|
||||
|
||||
genre.delete(session);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBooksPaginated() {
|
||||
Genre genre = new Genre("Thriller");
|
||||
genre.save(session);
|
||||
|
||||
Book book1 = new Book("Test title 1", false, genre);
|
||||
book1.save(session);
|
||||
Book book2 = new Book("Test title 2", true, genre);
|
||||
book2.save(session);
|
||||
Book book3 = new Book("Test title 3", false, genre);
|
||||
book3.save(session);
|
||||
|
||||
|
||||
List<Book> books = new ArrayList<>();
|
||||
books.add(book1);
|
||||
books.add(book2);
|
||||
books.add(book3);
|
||||
|
||||
// pierwsze dwa rekordy
|
||||
List<Book> retrievedBooks = Book.getBooksPaginated(session, 1, 2);
|
||||
assertNotNull(retrievedBooks);
|
||||
assertFalse(retrievedBooks.isEmpty());
|
||||
for (Book rb : retrievedBooks) {
|
||||
assertTrue(books.contains(rb));
|
||||
}
|
||||
|
||||
// wszystkie rekordy
|
||||
retrievedBooks = Book.getBooksPaginated(session, 1, 3);
|
||||
assertNotNull(retrievedBooks);
|
||||
assertEquals(retrievedBooks, books);
|
||||
|
||||
book1.delete(session);
|
||||
book2.delete(session);
|
||||
book3.delete(session);
|
||||
genre.delete(session);
|
||||
|
||||
retrievedBooks = Book.getBooksPaginated(session, 1, 3);
|
||||
assertNotNull(retrievedBooks);
|
||||
assertTrue(retrievedBooks.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBooksPaginatedError() {
|
||||
assertNull(Book.getBooksPaginated(session, -2, 0));
|
||||
assertNull(Book.getBooksPaginated(session, 1, -5));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
import org.hibernate.Session;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.mikgul.Genre;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
public class TestGenre {
|
||||
private Session session;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
try {
|
||||
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-dynamic");
|
||||
|
||||
EntityManager entityManager = entityManagerFactory.createEntityManager();
|
||||
this.session = entityManager.unwrap(Session.class);
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("Creating session failed." + ex);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (this.session != null) {
|
||||
this.session.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCRUD() {
|
||||
Genre genre = new Genre("Fantasy");
|
||||
|
||||
// CREATE
|
||||
genre.save(session);
|
||||
assertNotNull(genre.getId());
|
||||
|
||||
// READ
|
||||
Genre retrievedGenre = Genre.getById(session, genre.getId());
|
||||
assertNotNull(retrievedGenre);
|
||||
assertEquals(genre.getName(), retrievedGenre.getName());
|
||||
|
||||
// UPDATE
|
||||
retrievedGenre.setName("Dark-Fantasy");
|
||||
retrievedGenre.update(session);
|
||||
Genre updatedGenre = Genre.getById(session, retrievedGenre.getId());
|
||||
assertNotNull(updatedGenre);
|
||||
assertEquals("Dark-Fantasy", updatedGenre.getName());
|
||||
|
||||
// DELETE
|
||||
genre.delete(session);
|
||||
Genre deletedGenre = Genre.getById(session, genre.getId());
|
||||
assertNull(deletedGenre);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue