-
Notifications
You must be signed in to change notification settings - Fork 19
Johan #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Johan #13
Changes from all commits
cd58553
f043eb4
86c2ac2
5c33218
c5b0d5f
3f3262f
5125f3a
bc6883c
4d51d1f
be0e7b4
5427500
0135fb8
cc6a16e
719fa14
27cc14b
d253f70
438577a
b27871c
41fcca0
02a6b0e
0066a05
9bf2aca
55889b0
887f1e1
ad87eed
daa00c8
effd2b1
d330599
01bf50a
3c10c42
9b8ceaa
109988c
57b7d4e
b043f55
e2f2c57
42d834d
cfde7f8
ba087fb
a6de7d4
a1beeb5
e91d463
e9fb1ae
4929c8f
1ea47d7
af12454
cff7dee
3d148c3
5dff5c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package org.example; | ||
|
|
||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.MappedSuperclass; | ||
|
|
||
| @MappedSuperclass | ||
| public abstract class BaseEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package org.example; | ||
|
|
||
| import jakarta.persistence.EntityManager; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public class BaseRepositoryImpl<T extends org.example.BaseEntity> implements Repository<T> { | ||
|
|
||
| protected EntityManager em; | ||
| protected Class<T> entityClass; | ||
|
|
||
|
|
||
| public BaseRepositoryImpl(Class<T> entityClass) { | ||
| this.entityClass = entityClass; | ||
| } | ||
|
|
||
| public void setEntityManager(EntityManager em) { | ||
| this.em = em; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| @Override | ||
| public T save(T entity) { | ||
| if (entity == null) { | ||
| throw new IllegalArgumentException("Entity cannot be null"); | ||
| } | ||
| if (entity.getId() == null) { | ||
| em.persist(entity); | ||
| return entity; | ||
| } else { | ||
| return em.merge(entity); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public EntityManager getEntityManager() { | ||
| return em; | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<T> findById(Long id) { | ||
| return Optional.ofNullable(em.find(entityClass, id)); | ||
| } | ||
|
|
||
| @Override | ||
| public void delete(T entity) { | ||
| em.remove(em.contains(entity) ? entity : em.merge(entity)); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteById(Long id) { | ||
| T entity = em.find(entityClass, id); | ||
| if (entity != null) { | ||
| em.remove(entity); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Iterable<T> findAll() { | ||
| return em.createQuery( | ||
| "select e from " + entityClass.getSimpleName() + " e", entityClass | ||
| ).getResultList(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean existsById(Long id) { | ||
| Long count = em.createQuery( | ||
| "select count(e) from " + entityClass.getSimpleName() + " e where e.id = :id", | ||
| Long.class | ||
| ).setParameter("id", id).getSingleResult(); | ||
| return count > 0; | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public long count() { | ||
|
|
||
| return em.createQuery("select count(e) from " + entityClass.getSimpleName() + " e", Long.class).getSingleResult(); | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() { | ||
| em.flush(); | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| em.clear(); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,6 @@ | ||||||||||||||||||
| package org.example; | ||||||||||||||||||
|
|
||||||||||||||||||
| public class BusinessException extends Throwable { | ||||||||||||||||||
| public BusinessException(String s) { | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+3
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exception message is discarded; consider extending Two issues:
🐛 Proposed fix-public class BusinessException extends Throwable {
- public BusinessException(String s) {
- }
+public class BusinessException extends Exception {
+ public BusinessException(String message) {
+ super(message);
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
| } | ||||||||||||||||||
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.
EntityManager can be null when methods are called.
The
emfield is only set viasetEntityManager(), but all methods use it without null checks. If a caller forgets to set the EntityManager, any operation will throwNullPointerException.Consider either:
🐛 Option 1: Require EntityManager in constructor
📝 Committable suggestion
🤖 Prompt for AI Agents