#Repo patern in java

1 messages · Page 1 of 1 (latest)

quiet tundra
#

Hi, some basically I'm strugling with something that might be very basic, yet I'm stuck. I need to map 2 different entities coming each from a different repository. I see theories that you need to do that in the repository itself, which I don't see how that would be possible. Any examples?

#

@prime osprey , here is the thread

#

So I got a list of services, but each service has it's own repository

#

so let's use a simple example, let's say I got a class House, and a list of furniture

prime osprey
#

Are you using any ORMs like Hibernate?

quiet tundra
#

No, frameworks

prime osprey
#

Are we speaking of the JPA specifications?

quiet tundra
#

not sure what you talk about tbh

prime osprey
#

The Java Persistence API. Its a specification for exactly this.

quiet tundra
#

No I don't use any of that

#

To me it wouldn't make much sense to do the mapping in the service, but also not in the Repo

prime osprey
#

Alright. Every entity has an ID.
If you have a Hous which has its own table and
Furniture which has its own table, then you would
map it by using a collection of IDs.

If your ID is an int then a Collection<Integer> or if its a UUID then
a Collection<UUID>. Just a more concrete implementation like List or Set.

quiet tundra
#

Yes I understand, but ideally, you end up with both a database identifier like an id, and eventually, the goal would be that you end up with linked objects no?

#

So that I can do

House house = new House();
Door door = house.getDoor();
door.open();

#

Maybe a stupid example

#

so I would have 2 different repos, one for the house, and one for the door

#

when and where would I map these together

#

so maybe a specific code example:

prime osprey
# quiet tundra when and where would I map these together

What do you mean by "map these together"
You have 2 repositories. One for House and one for Door.
The House does not contain a Door but just an ID for a door.
When you call getDoor() then you fetch the door from the other repository.

There are 2 ways of fetching:
Lazy = first time getDoor() is called you retrieve the Door object
Eager = the door is loaded when then House is loaded

#

Usually you would do all of this using the Proxy pattern. (If you would use Hibernate or another ORM)

quiet tundra
#

I will write a code example

prime osprey
#

brb 10min

quiet tundra
#
public class A{

House house = this.houseService.getHouse("myhouse");
Door door = house.getDoor(); <-- this would not be possible then. It would be the ID of the house instead
}
#

alright, take ur time

prime osprey
#

Thats how you would do it with relation mapping

quiet tundra
#

yea but that would not be possible

#

unless I am wrong

#

but the Door object is a model

#

and the model should not have dependency on Services

#

I made this small example drawing

#

the model should never interact with the repo

#

only the other way around

#

@prime osprey , does this example help?

#

so getDoor() should no have any service calls

prime osprey
#

ill show you what i mean later. Thats why orms use a proxy layer

quiet tundra
#

so then the question is, where would I map together the 2, because when I call the house service, to fetch my house, I expect it comes with a door

prime osprey
quiet tundra
#

So something like this?

class HouseService {

private final DoorService doorService;
public HouseService(DoorService doorService){
this.doorService = doorService;
}

House house = new House();
Door door = this.doorService.getDoorById(house.getDoorID());
}
prime osprey
#

The model does not need to be exposed to the client. You can have a business Entity which
can interact with multiple repositories

quiet tundra
#

I know it's not valid java

prime osprey
#

In 1 to 1 (bidirectional) relations

quiet tundra
#

But not if I use the ID's right?

prime osprey
quiet tundra
#

so what you are saying, is instead of having a getter getDoor() and fetch a door object. I fetch the door object itself based on the doorId in the house?

prime osprey
#

What if Door has a field to get the House it is owned by?

quiet tundra
#

Yes indeed

#

But if I would fetch the objects each seperate, then that would not be an issue anymore

#

I see what you are saying now

#

So basically, I don't return an object that has an actual reference relationship

#

I return the id's, and fetch the objects based on the ID through the approriate service

prime osprey
#

Thats why you would use an aggregator that has both the HouseService and DoorService.

quiet tundra