generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
7777544
commit 5e11c0c
Showing
12 changed files
with
192 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 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,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(); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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,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); | ||
} | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.