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
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
61 deletions
+44
-61
AbstractHibernateRepository.java
...ain/java/com/jh/boot/jpa/AbstractHibernateRepository.java
+35
-60
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
;
package
com
.
jh
.
boot
.
jpa
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Collection
;
import
java.util.HashSet
;
import
java.util.HashMap
;
import
java.util.Set
;
import
java.util.Iterator
;
import
java.util.function.Function
;
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.EntityManager
;
import
javax.persistence.NoResultException
;
import
javax.persistence.NoResultException
;
...
@@ -84,75 +89,45 @@ public abstract class AbstractHibernateRepository {
...
@@ -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
;
}
//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
));
}
StringBuilder
sb
=
new
StringBuilder
(
"delete from "
);
protected
<
T
extends
AbstractIdEntity
>
void
synchronizeCollections
(
Collection
<
T
>
currentCollection
,
Collection
<
T
>
newCollection
,
BiConsumer
<
T
,
T
>
modifyFn
,
Consumer
<
T
>
modifyRelationship
,
boolean
deleteOrphans
)
{
sb
.
append
(
referencedClass
).
append
(
" ref"
);
Objects
.
requireNonNull
(
currentCollection
);
sb
.
append
(
" where ref."
).
append
(
referencedField
).
append
(
" = :root"
);
Objects
.
requireNonNull
(
newCollection
);
Objects
.
requireNonNull
(
modifyFn
);
if
(!
elements
.
isEmpty
())
{
List
<
T
>
toBeAdded
=
new
ArrayList
<>();
sb
.
append
(
" and ref not in (:refs)"
);
Map
<
Long
,
T
>
map
=
new
HashMap
<>();
}
if
(
newCollection
!=
null
)
{
for
(
T
nc
:
newCollection
)
{
Query
q
=
entityManager
.
createQuery
(
sb
.
toString
());
if
(
nc
.
getId
()
==
null
)
{
q
.
setParameter
(
"root"
,
entity
);
toBeAdded
.
add
(
nc
);
if
(!
elements
.
isEmpty
())
{
}
else
{
q
.
setParameter
(
"refs"
,
elements
);
map
.
put
(
nc
.
getId
(),
nc
);
}
q
.
executeUpdate
();
if
(
references
!=
null
)
{
for
(
S
s
:
references
)
{
save
(
s
);
}
}
}
}
}
}
Map
<
Long
,
T
>
newMap
=
map
;
/**
Iterator
<
T
>
it
=
currentCollection
.
iterator
();
* Sync references.
while
(
it
.
hasNext
())
{
*
T
t
=
it
.
next
();
* @param <T> the generic type
T
nc
=
newMap
.
get
(
t
.
getId
());
* @param <S> the generic type
if
(
nc
==
null
)
{
* @param entities the entities
it
.
remove
();
* @param referencedClass the referenced class
if
(
deleteOrphans
)
{
* @param referencedField the referenced field
delete
(
t
);
* @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
;
}
}
for
(
T
entity
:
entities
)
{
if
(
entity
.
getId
()
==
null
)
{
continue
;
continue
;
}
}
syncReferences
(
entity
,
referencedClass
,
referencedField
,
valueProviders
.
apply
(
entity
));
modifyFn
.
accept
(
t
,
nc
);
}
}
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;
...
@@ -4,6 +4,7 @@ import java.util.ArrayList;
import
java.util.Collection
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.function.Function
;
import
java.util.function.Function
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
...
@@ -23,7 +24,14 @@ public class Converter {
...
@@ -23,7 +24,14 @@ public class Converter {
return
mappingFunction
.
apply
(
source
);
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
)
{
if
(
list
==
null
)
{
return
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