This repository was archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Completed solution #1
Open
tboychuk
wants to merge
47
commits into
master
Choose a base branch
from
exercise/completed
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+283
−119
Open
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
c5f1780
Complete exercise AccountDao
tboychuk 84aa6f6
Merge branch 'master' into exercise/completed
tboychuk ede6d59
Complete the exercise "JPA persistence.xml configs"
tboychuk 1834ca2
Merge branch 'master' into exercise/completed
tboychuk 365cf52
Merge branch 'master' into exercise/completed
tboychuk 2e8eace
Merge branch 'master' into exercise/completed
tboychuk 98607f1
Fix exercise URLs
tboychuk 72add57
Merge branch 'master' into exercise/completed
tboychuk 285ff6d
Merge branch 'master' into exercise/completed
tboychuk d48f4b9
Merge branch 'master' into exercise/completed
tboychuk 5aa7af4
Merge branch 'master' into exercise/completed
tboychuk 7585339
Complete "Hello JPA entity" exercise
tboychuk e8cf5cc
Merge branch 'master' into exercise/completed
tboychuk d8f4dca
Complete "Employee profile" exercise
tboychuk 2f999ca
Merge branch 'master' into exercise/completed
tboychuk 6fd84dd
Merge branch 'master' into exercise/completed
tboychuk 9a3573b
Complete "Photo comment DAO" exercise
tboychuk 8e94609
Merge branch 'master' into exercise/completed
tboychuk 47258de
Complete "Authors & Books" exercise
tboychuk 9aa8d38
Merge branch 'master' into exercise/completed
tboychuk 26ec6bf
Complete "QueryHelper" exercise
tboychuk b402233
Merge branch 'master' into exercise/completed
tboychuk eeba5a6
Complete "Company products" exercise
tboychuk 80f5754
Merge branch 'master' into exercise/completed
tboychuk e9ae78d
Merge branch 'master' into exercise/completed
tboychuk 5af6bfe
Implement PhotoDaoImpl#addComment()
tboychuk 6af1ac1
Merge branch 'master' into exercise/completed
tboychuk 1994add
Update Photo
tboychuk 55d97d4
Update Author#equals() method
tboychuk 6cf3e8b
Update Author#equals() method
tboychuk 68b2277
Merge branch 'master' into exercise/completed
tboychuk 38c7818
Specify JoinColumn for EmployeeProfile.java
tboychuk 5655f95
Merge branch 'master' into exercise/completed
tboychuk 1e70550
Fix Photo.java
tboychuk 49f3f98
Add a comment to PhotoDaoImpl.java
tboychuk 64e038f
Remove redundant code from PhotoDaoImpl.java
tboychuk 83f970f
Merge branch 'master' into exercise/completed
tboychuk e0b5bb5
Merge branch 'master' into exercise/completed
tboychuk d087b6e
Merge branch 'master' into exercise/completed
tboychuk 1310577
Simplified AccountDaoImpl.java
tboychuk 8544333
Upgrade AccountDaoImpl.java
tboychuk 633c8ce
Merge branch 'master' into exercise/completed
tboychuk a6b6eb8
Add natural key mapping to Book.java
tboychuk 8c5b904
Upgrade Java to 11 and other major dependencies
tboychuk b2f6f29
Merge branch 'master' into exercise/completed
tboychuk adeb2e6
Merge branch 'master' into exercise/completed
tboychuk d35a60f
Merge branch 'master' into exercise/completed
tboychuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,62 @@ | ||
package com.bobocode.model; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import javax.persistence.CascadeType; | ||
import javax.persistence.*; | ||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
/** | ||
* todo: | ||
* - implement no arguments constructor | ||
* - implement getters and setters for all fields | ||
* - implement equals() based on identifier field | ||
* - implement hashCode() that return constant value 31 | ||
* - make setter for field {@link Author#books} private | ||
* - initialize field {@link Author#books} as new {@link HashSet} | ||
* - implement a helper {@link Author#addBook(Book)} that establishes a relation on both sides | ||
* - implement a helper {@link Author#removeBook(Book)} that drops a relation on both sides | ||
* <p> | ||
* - configure JPA entity | ||
* - specify table name: "author" | ||
* - configure auto generated identifier | ||
* - configure mandatory column "first_name" for field {@link Author#firstName} | ||
* - configure mandatory column "last_name" for field {@link Author#lastName} | ||
* <p> | ||
* - configure many-to-many relation between {@link Author} and {@link Book} | ||
* - configure cascade operations for this relations {@link CascadeType#PERSIST} and {@link CascadeType#MERGE} | ||
* - configure link (join) table "author_book" | ||
* - configure foreign key column "book_id" references book table | ||
* - configure foreign key column "author_id" references author table | ||
*/ | ||
@NoArgsConstructor | ||
@Getter | ||
@Setter | ||
@Entity | ||
@Table(name = "author") | ||
public class Author { | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Column(name = "first_name", nullable = false) | ||
private String firstName; | ||
|
||
@Column(name = "last_name", nullable = false) | ||
private String lastName; | ||
private Set<Book> books; | ||
|
||
@Setter(AccessLevel.PRIVATE) | ||
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) | ||
@JoinTable(name = "author_book", | ||
joinColumns = @JoinColumn(name = "author_id"), | ||
inverseJoinColumns = @JoinColumn(name = "book_id") | ||
) | ||
private Set<Book> books = new HashSet<>(); | ||
|
||
public void addBook(Book book) { | ||
throw new UnsupportedOperationException("Are you kidding me?"); | ||
books.add(book); | ||
book.getAuthors().add(this); | ||
} | ||
|
||
public void removeBook(Book book) { | ||
throw new UnsupportedOperationException("Are you kidding me?"); | ||
books.remove(book); | ||
book.getAuthors().remove(this); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof Author)) return false; | ||
|
||
Author author = (Author) o; | ||
|
||
return Objects.equals(id, author.id); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return 31; | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,32 @@ | ||
package com.bobocode.model; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.*; | ||
import org.hibernate.annotations.NaturalId; | ||
|
||
import javax.persistence.*; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* todo: | ||
* - implement no arguments constructor | ||
* - implement getters and setters for all fields | ||
* - implement equals() and hashCode() based on {@link Book#isbn} | ||
* - make setter for field {@link Book#authors} private | ||
* - initialize field {@link Book#authors} as new {@link HashSet} | ||
* <p> | ||
* - configure JPA entity | ||
* - specify table name: "book" | ||
* - configure auto generated identifier | ||
* - configure mandatory column "name" for field {@link Book#name} | ||
* - configure mandatory unique column "isbn" for field {@link Book#isbn}, it is a natural key candidate | ||
* <p> | ||
* - configure many-to-many relation as mapped on the {@link Author} side | ||
*/ | ||
@NoArgsConstructor | ||
@Getter | ||
@Setter | ||
@EqualsAndHashCode(of = "isbn") | ||
@Entity | ||
@Table(name = "book") | ||
public class Book { | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
private String name; | ||
|
||
@NaturalId | ||
@Column(nullable = false, unique = true) | ||
private String isbn; | ||
private Set<Author> authors; | ||
|
||
@Setter(AccessLevel.PRIVATE) | ||
@ManyToMany(mappedBy = "books") | ||
private Set<Author> authors = new HashSet<>(); | ||
} | ||
|
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
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
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
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
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
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
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
21 changes: 12 additions & 9 deletions
21
hello-persistence-xml/src/main/resources/META-INF/persistence.xml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1"> | ||
|
||
<!--todo: configure a persistence unit name: TuttiFrutti--> | ||
<!--todo: add class Song to this persistence unit--> | ||
<!--todo: configure a hibernate connection url for in-memory H2 database--> | ||
<!--database name: tutti_frutti_db--> | ||
<!--database additional properties: DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false--> | ||
<!--todo: configure a hibernate connection username: little_richard--> | ||
<!--todo: configure a hibernate connection password: rock_n_roll_is_alive--> | ||
<!--todo: configure a hibernate SQL dialect for H2 database--> | ||
<!--todo: configure hibernate generate DDL and create database--> | ||
<persistence-unit name="TuttiFrutti"> | ||
<class>com.bobocode.model.Song</class> | ||
|
||
<properties> | ||
<property name="hibernate.connection.url" value="jdbc:h2:mem:tutti_frutti_db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false"/> | ||
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/> | ||
<property name="hibernate.connection.username" value="little_richard"/> | ||
<property name="hibernate.connection.password" value="rock_n_roll_is_alive"/> | ||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> | ||
<property name="hibernate.hbm2ddl.auto" value="create"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
</persistence> |
32 changes: 22 additions & 10 deletions
32
photo-comment-dao/src/main/java/com/bobocode/dao/PhotoDaoImpl.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,54 @@ | ||
package com.bobocode.dao; | ||
|
||
import com.bobocode.model.Photo; | ||
import com.bobocode.model.PhotoComment; | ||
import com.bobocode.util.EntityManagerUtil; | ||
|
||
import javax.persistence.EntityManagerFactory; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Please note that you should not use auto-commit mode for your implementation. | ||
*/ | ||
public class PhotoDaoImpl implements PhotoDao { | ||
private EntityManagerFactory entityManagerFactory; | ||
private EntityManagerUtil emUtil; | ||
|
||
public PhotoDaoImpl(EntityManagerFactory entityManagerFactory) { | ||
this.entityManagerFactory = entityManagerFactory; | ||
this.emUtil = new EntityManagerUtil(entityManagerFactory); | ||
} | ||
|
||
@Override | ||
public void save(Photo photo) { | ||
throw new UnsupportedOperationException("Just do it!"); // todo | ||
Objects.requireNonNull(photo); | ||
emUtil.performWithinTx(entityManager -> entityManager.persist(photo)); | ||
} | ||
|
||
@Override | ||
public Photo findById(long id) { | ||
throw new UnsupportedOperationException("Just do it!"); // todo | ||
return emUtil.performReturningWithinTx(entityManager -> entityManager.find(Photo.class, id)); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public List<Photo> findAll() { | ||
throw new UnsupportedOperationException("Just do it!"); // todo | ||
return emUtil.performReturningWithinTx( | ||
entityManager -> entityManager.createQuery("select p from Photo p").getResultList() | ||
); | ||
} | ||
|
||
@Override | ||
public void remove(Photo photo) { | ||
throw new UnsupportedOperationException("Just do it!"); // todo | ||
Objects.requireNonNull(photo); | ||
emUtil.performWithinTx(entityManager -> { | ||
Photo managedPhoto = entityManager.merge(photo); | ||
entityManager.remove(managedPhoto); | ||
}); | ||
} | ||
|
||
@Override | ||
public void addComment(long photoId, String comment) { | ||
throw new UnsupportedOperationException("Just do it!"); // todo | ||
emUtil.performWithinTx(entityManager -> { | ||
Photo photoReference = entityManager.getReference(Photo.class, photoId);// does not call database | ||
PhotoComment photoComment = new PhotoComment(comment, photoReference); | ||
entityManager.persist(photoComment); | ||
}); | ||
} | ||
} |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Хочу уточнить, Merge вроде как возвращает объект с базы в persistence context, а как он выполняет апдейт с его помощью?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since provided account is in the detached state (it has an id, and is not loaded to the Persistence Context), Hibernate will call the database to load this account by its id first. Then it will copy data from account you provided onto loaded account. So, if the account you provided has some fields changed, these changes will be applied to the loaded account, and since loaded account is changed in the scope of a persistence context, dirty checking will call database to perform an update.