Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jh-boot
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jan Hrabal
jh-boot
Commits
df9deb94
Commit
df9deb94
authored
Oct 21, 2019
by
Jan Hrabal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
w
parent
c2bde5c5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
66 deletions
+49
-66
AbstractHibernateRepository.java
...ain/java/com/jh/boot/jpa/AbstractHibernateRepository.java
+40
-65
Converter.java
src/main/java/com/jh/boot/web/Converter.java
+9
-1
No files found.
src/main/java/com/jh/boot/jpa/AbstractHibernateRepository.java
View file @
df9deb94
package
com
.
jh
.
boot
.
jpa
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.HashSet
;
import
java.util.Set
;
import
java.util.function.Function
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Objects
;
import
java.util.function.BiConsumer
;
import
java.util.function.Consumer
;
import
javax.persistence.EntityManager
;
import
javax.persistence.NoResultException
;
...
...
@@ -84,75 +89,45 @@ public abstract class AbstractHibernateRepository {
}
}
/**
* Sync references.
*
* @param <T> the generic type
* @param <S> the generic type
* @param entity the entity
* @param referencedClass the referenced class
* @param referencedField the referenced field
* @param references the references
*/
//FIXME??
protected
<
T
extends
AbstractIdEntity
,
S
extends
AbstractIdEntity
>
void
syncReferences
(
T
entity
,
String
referencedClass
,
String
referencedField
,
Collection
<
S
>
references
)
{
if
(
entity
.
getId
()
==
null
)
{
//nothing to do
return
;
}
protected
<
T
extends
AbstractIdEntity
>
void
synchronizeCollections
(
Collection
<
T
>
currentCollection
,
Collection
<
T
>
newCollection
,
BiConsumer
<
T
,
T
>
modifyFn
,
Consumer
<
T
>
modifyRelationship
,
boolean
deleteOrphans
)
{
Objects
.
requireNonNull
(
currentCollection
);
Objects
.
requireNonNull
(
newCollection
);
Objects
.
requireNonNull
(
modifyFn
);
//filter only already stored references
Set
<
S
>
elements
=
new
HashSet
<>();
if
(
references
!=
null
)
{
references
.
stream
().
filter
(
e
->
e
.
getId
()
!=
null
).
forEach
(
e
->
elements
.
add
(
e
));
List
<
T
>
toBeAdded
=
new
ArrayList
<>();
Map
<
Long
,
T
>
map
=
new
HashMap
<>();
if
(
newCollection
!=
null
)
{
for
(
T
nc
:
newCollection
)
{
if
(
nc
.
getId
()
==
null
)
{
toBeAdded
.
add
(
nc
);
}
else
{
map
.
put
(
nc
.
getId
(),
nc
);
}
}
}
StringBuilder
sb
=
new
StringBuilder
(
"delete from "
);
sb
.
append
(
referencedClass
).
append
(
" ref"
);
sb
.
append
(
" where ref."
).
append
(
referencedField
).
append
(
" = :root"
);
if
(!
elements
.
isEmpty
())
{
sb
.
append
(
" and ref not in (:refs)"
);
}
Map
<
Long
,
T
>
newMap
=
map
;
Query
q
=
entityManager
.
createQuery
(
sb
.
toString
());
q
.
setParameter
(
"root"
,
entity
);
if
(!
elements
.
isEmpty
())
{
q
.
setParameter
(
"refs"
,
elements
);
}
Iterator
<
T
>
it
=
currentCollection
.
iterator
();
while
(
it
.
hasNext
())
{
T
t
=
it
.
next
();
T
nc
=
newMap
.
get
(
t
.
getId
());
if
(
nc
==
null
)
{
it
.
remove
();
if
(
deleteOrphans
)
{
delete
(
t
);
}
continue
;
}
q
.
executeUpdate
();
if
(
references
!=
null
)
{
for
(
S
s
:
references
)
{
save
(
s
);
}
}
}
/**
* Sync references.
*
* @param <T> the generic type
* @param <S> the generic type
* @param entities the entities
* @param referencedClass the referenced class
* @param referencedField the referenced field
* @param valueProviders the value providers
*/
protected
<
T
extends
AbstractIdEntity
,
S
extends
AbstractIdEntity
>
void
syncReferences
(
Collection
<
T
>
entities
,
String
referencedClass
,
String
referencedField
,
Function
<
T
,
?
extends
Collection
<
S
>>
valueProviders
)
{
if
(
entities
==
null
||
entities
.
isEmpty
())
{
//nothing to do
return
;
modifyFn
.
accept
(
t
,
nc
);
}
for
(
T
entity
:
entities
)
{
if
(
entity
.
getId
()
==
null
)
{
continue
;
}
syncReferences
(
entity
,
referencedClass
,
referencedField
,
valueProviders
.
apply
(
entity
));
for
(
T
tba
:
toBeAdded
)
{
save
(
tba
);
}
}
}
src/main/java/com/jh/boot/web/Converter.java
View file @
df9deb94
...
...
@@ -4,6 +4,7 @@ import java.util.ArrayList;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.function.Function
;
import
java.util.stream.Collectors
;
...
...
@@ -23,7 +24,14 @@ public class Converter {
return
mappingFunction
.
apply
(
source
);
}
public
static
<
S
,
D
>
List
<
D
>
convertList
(
List
<
S
>
list
,
Function
<
S
,
D
>
mappingFunction
)
{
public
static
<
S
,
D
>
Set
<
D
>
convertSet
(
Collection
<
S
>
list
,
Function
<
S
,
D
>
mappingFunction
)
{
if
(
list
==
null
)
{
return
null
;
}
return
list
.
stream
().
map
(
mappingFunction
).
collect
(
Collectors
.
toSet
());
}
public
static
<
S
,
D
>
List
<
D
>
convertList
(
Collection
<
S
>
list
,
Function
<
S
,
D
>
mappingFunction
)
{
if
(
list
==
null
)
{
return
null
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment