Skip to content

Commit

Permalink
1. Config hibernate.cfg.xml
Browse files Browse the repository at this point in the history
2. AbstractDao - Use Generic Type for reformat code
   - add create, get, getAll and remove method`s and impl them
3. CommentDaoImpl - impl all method
4. Comment - use Entity
5. Message - use Entity and Cascade
6. MassageDaoImpl - impl create method and get, getAll, remove
7. MessageDetails - use Entity
8. MessageDetailsDaoImpl - impl all method
9. pom - add maven dependency
10. SmileDaoImpl - impl all method
11. User - use Entity and cascade
12. UserDaoImpl - impl method create and get, getAll, remove. =)
  • Loading branch information
MaksimSokur committed May 26, 2024
1 parent 7777544 commit 5e11c0c
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 30 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<version>2.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-core</artifactId>
<version>4.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
56 changes: 54 additions & 2 deletions src/main/java/core/basesyntax/dao/impl/AbstractDao.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,63 @@
package core.basesyntax.dao.impl;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public abstract class AbstractDao {
public abstract class AbstractDao<T> {
protected final SessionFactory factory;
private final Class<T> clazz;

protected AbstractDao(SessionFactory sessionFactory) {
protected AbstractDao(SessionFactory sessionFactory, Class<T> clazz) {
this.clazz = clazz;
this.factory = sessionFactory;
}

public T create(T entity) {
Session session = factory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(entity);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create entity ", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

public T get(Long id) {
return (T) factory.openSession().get(clazz, id);
}

public List<T> getAll() {
return factory.openSession().createQuery("FROM " + clazz.getSimpleName(), clazz).list();
}

public void remove(T entity) {
Session session = factory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.delete(entity);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't delete entity ", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
import java.util.List;
import org.hibernate.SessionFactory;

public class CommentDaoImpl extends AbstractDao implements CommentDao {
public class CommentDaoImpl extends AbstractDao<Comment> implements CommentDao {
public CommentDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
super(sessionFactory, Comment.class);
}

@Override
public Comment create(Comment entity) {
return null;
return super.create(entity);
}

@Override
public Comment get(Long id) {
return null;
return super.get(id);
}

@Override
public List<Comment> getAll() {
return null;
return super.getAll();
}

@Override
public void remove(Comment entity) {

super.remove(entity);
}
}
34 changes: 28 additions & 6 deletions src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,53 @@

import core.basesyntax.dao.MessageDao;
import core.basesyntax.model.Message;
import core.basesyntax.model.MessageDetails;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class MessageDaoImpl extends AbstractDao implements MessageDao {
public class MessageDaoImpl extends AbstractDao<Message> implements MessageDao {
public MessageDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
super(sessionFactory, Message.class);
}

@Override
public Message create(Message entity) {
return null;
Session session = factory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();

MessageDetails messageDetails = entity.getMessageDetails();
if (messageDetails != null) {
session.save(messageDetails);
}
session.save(entity);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create entity ", e);
} finally {
session.close();
}
return entity;
}

@Override
public Message get(Long id) {
return null;
return super.get(id);
}

@Override
public List<Message> getAll() {
return null;
return super.getAll();
}

@Override
public void remove(Message entity) {

super.remove(entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
import core.basesyntax.model.MessageDetails;
import org.hibernate.SessionFactory;

public class MessageDetailsDaoImpl extends AbstractDao implements MessageDetailsDao {
public class MessageDetailsDaoImpl
extends AbstractDao<MessageDetails>
implements MessageDetailsDao {
public MessageDetailsDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
super(sessionFactory, MessageDetails.class);
}

@Override
public MessageDetails create(MessageDetails entity) {
return null;
return super.create(entity);
}

@Override
public MessageDetails get(Long id) {
return null;
return super.get(id);
}
}
10 changes: 5 additions & 5 deletions src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
import java.util.List;
import org.hibernate.SessionFactory;

public class SmileDaoImpl extends AbstractDao implements SmileDao {
public class SmileDaoImpl extends AbstractDao<Smile> implements SmileDao {
public SmileDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
super(sessionFactory, Smile.class);
}

@Override
public Smile create(Smile entity) {
return null;
return super.create(entity);
}

@Override
public Smile get(Long id) {
return null;
return super.get(id);
}

@Override
public List<Smile> getAll() {
return null;
return super.getAll();
}
}
37 changes: 31 additions & 6 deletions src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,57 @@
package core.basesyntax.dao.impl;

import core.basesyntax.dao.UserDao;
import core.basesyntax.model.Comment;
import core.basesyntax.model.User;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class UserDaoImpl extends AbstractDao implements UserDao {
public class UserDaoImpl extends AbstractDao<User> implements UserDao {
public UserDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
super(sessionFactory, User.class);
}

@Override
public User create(User entity) {
return null;
Session session = factory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();

if (entity.getComments() != null) {
for (Comment comment : entity.getComments()) {
session.save(comment);
}
}
session.save(entity);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create entity ", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public User get(Long id) {
return null;
return super.get(id);
}

@Override
public List<User> getAll() {
return null;
return super.getAll();
}

@Override
public void remove(User entity) {

super.remove(entity);
}
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/model/Comment.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package core.basesyntax.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import java.util.List;

@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;

@ManyToMany
@JoinTable(
name = "comment_smile",
joinColumns = @JoinColumn(name = "comment_id"),
inverseJoinColumns = @JoinColumn(name = "smile_id")
)

private List<Smile> smiles;

public Long getId() {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/core/basesyntax/model/Message.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
package core.basesyntax.model;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;

@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
@OneToOne(cascade = CascadeType.REMOVE)
private MessageDetails messageDetails;

public Message() {
}

public Long getId() {
return id;
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/model/MessageDetails.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package core.basesyntax.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.time.LocalDateTime;

@Entity
public class MessageDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String sender;
private LocalDateTime sentTime;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/core/basesyntax/model/User.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
package core.basesyntax.model;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import java.util.List;

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinTable(
name = "user_comment",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "comment_id")
)
private List<Comment> comments;

public Long getId() {
Expand Down
Loading

0 comments on commit 5e11c0c

Please sign in to comment.