projekt 2 finished
This commit is contained in:
parent
cc75c784b2
commit
5503971716
@ -1 +1,8 @@
|
||||
# Projekt 2
|
||||
|
||||
## Jak Rozpocząć?
|
||||
1. Pobierz repozytorium.
|
||||
2. Przejdź do głównego katalogu repozytorium.
|
||||
3. W terminalu wpisz `./start.sh`.
|
||||
4. Zostanie utworzony kontener Docker z bazą danych PostgreSQL, która zostanie zainicjalizowana danymi z pliku `init_db.sql`.
|
||||
5. Teraz można uruchomić plik `main.java`, który wykona operacje SELECT i INSERT na bazie danych.
|
@ -139,6 +139,7 @@ CREATE TABLE IF NOT EXISTS reviews (
|
||||
rating INT,
|
||||
review_text TEXT,
|
||||
author_name VARCHAR(100),
|
||||
created_at TIMESTAMP WITH TIME ZONE,
|
||||
FOREIGN KEY (book_id) REFERENCES books(id)
|
||||
);
|
||||
|
||||
|
@ -5,9 +5,6 @@ import org.hibernate.SessionFactory;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
@ -22,11 +19,11 @@ public class Main {
|
||||
MyQuery myQuery = new MyQuery(session);
|
||||
|
||||
// here, replace "test" with the title of the book you want to query
|
||||
String title1 = "test";
|
||||
String title1 = "1984";
|
||||
myQuery.getBookByExactTitle(title1);
|
||||
|
||||
// here, replace "test" with the title of the book you want to query
|
||||
String title2 = "test";
|
||||
String title2 = "Pride and Prejudice";
|
||||
myQuery.getBookDetailsWithAuthorByTitle(title2);
|
||||
|
||||
// here, set the book id you want to query
|
||||
@ -40,17 +37,29 @@ public class Main {
|
||||
try {
|
||||
tx = session.beginTransaction();
|
||||
// create a book
|
||||
Book book = MyQuery.createBook("test title 3");
|
||||
String bookTitle = "test book title";
|
||||
Book book = MyQuery.createBook(bookTitle);
|
||||
session.save(book);
|
||||
|
||||
// create an Author
|
||||
Author author = MyQuery.createAuthor("adam 3");
|
||||
String authorName = "Adam Gulczynski";
|
||||
Author author = MyQuery.createAuthor(authorName);
|
||||
session.save(author);
|
||||
//
|
||||
// create book details
|
||||
BookDetails bookDetails = MyQuery.createBookDetails("333123123", 123, book, author);
|
||||
|
||||
String isbn = "1234-0000332";
|
||||
int pageCount = 123;
|
||||
BookDetails bookDetails = MyQuery.createBookDetails(isbn, pageCount, book, author);
|
||||
session.save(bookDetails);
|
||||
//
|
||||
|
||||
// create a book review
|
||||
int rating = 5;
|
||||
String reviewText = "Great book!";
|
||||
String reviewerName = "Adam";
|
||||
Review review = MyQuery.createReview(rating, reviewText, book, reviewerName);
|
||||
session.save(review);
|
||||
|
||||
tx.commit();
|
||||
} catch (Exception e) {
|
||||
if (tx != null) {
|
||||
|
@ -102,8 +102,6 @@ public class MyQuery {
|
||||
Book book = new Book();
|
||||
book.setTitle(bookTitle);
|
||||
|
||||
// ZonedDateTime currentTimestamp = ZonedDateTime.now();
|
||||
|
||||
return book;
|
||||
}
|
||||
|
||||
@ -122,5 +120,19 @@ public class MyQuery {
|
||||
bookDetails.setPageCount(pageCount);
|
||||
return bookDetails;
|
||||
}
|
||||
|
||||
public static Review createReview(int rating, String reviewText, Book book, String authorName) {
|
||||
Review review = new Review();
|
||||
review.setBook(book);
|
||||
review.setRating(rating);
|
||||
review.setReviewText(reviewText);
|
||||
review.setAuthorName(authorName);
|
||||
|
||||
ZonedDateTime currentTime = ZonedDateTime.now();
|
||||
review.setCreatedAt(currentTime);
|
||||
|
||||
return review;
|
||||
}
|
||||
|
||||
// ======================== END of INSERTing =================================
|
||||
}
|
||||
|
@ -2,11 +2,13 @@ package org.example;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
@Entity
|
||||
@Table(name = "reviews")
|
||||
public class Review {
|
||||
|
||||
@Id
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "review_id")
|
||||
private Long id;
|
||||
@ -23,6 +25,9 @@ public class Review {
|
||||
@Column(name = "author_name")
|
||||
private String authorName;
|
||||
|
||||
@Column(name = "created_at", columnDefinition = "TIMESTAMP WITH TIME ZONE")
|
||||
private ZonedDateTime createdAt;
|
||||
|
||||
public Review() {
|
||||
}
|
||||
|
||||
@ -66,6 +71,18 @@ public class Review {
|
||||
this.authorName = authorName;
|
||||
}
|
||||
|
||||
public ZonedDateTime getCreatedAt() {
|
||||
if (createdAt == null) {
|
||||
createdAt = ZonedDateTime.now();
|
||||
}
|
||||
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(ZonedDateTime createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Review{" +
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user