1

I have an existing java application in which I use now spring-boot and data jpa to save some data in a database. In one class Order which I convert now to an @Entity I have a member which is a List<Position>. Following is the code of the reduced classes

@Entity
public class Order
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    private List<Position> positions;

    //some other members follow here...
}

@Entity
public class Position
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    //some members follow here...
}

So what I have done is the following, I added the annotation @Transient to my list in Order and add inPosition a reference to an Order:

@Entity
public class Order
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @Transient
    private List<Position> positions;

    //some other members follow here...
}

@Entity
public class Position
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @ManyToOne
    private Order order;

    //some members follow here...
}

Now when I want to save an Order object, then I save first the Order in the corresponding repository and then go through the list of Positions and set in ervery the reference to Order and then save the Position object to its corresponding repository. If I want to fetch an Order then I fetch first the Order and then fetch the Positions in the correspoding repository with findByOrder(..).

So far this works. Now I'm facing the problem, if the application modifies in the Order the list with the Positions and I have to update the database with the new Order object, then I find no smooth solution to delete the Positions in the repository which were removed from the list by the application (as I have no longer a reference to the removed ones). I could delete first all Positions of that Order and then save the existing ones again.

So my questions is maybe if there is a better way to remove the Positions in the repository which were removed by the application. But maybe it would be an XY question, cause my approach how to save the Position-List is the reason why I am facing this problem. I appreciate any hints concering this.

Semaphor
  • 900
  • 2
  • 12
  • 33
  • "cause my approach how to save the Position-List is the reason why I am facing this problem." Exactly. The only question is why are you doing any of this? – Alan Hay Feb 17 '16 at 14:32
  • Well that question is simple to answer: I don't know how to do it better ;-) – Semaphor Feb 17 '16 at 15:47
  • Well this is a basic mapping so I'd suggest reviewing the docs. The answer below should point you in the right direction. – Alan Hay Feb 17 '16 at 16:07

1 Answers1

0

You're not doing it right. First, it's not clear why you're making the @OneToMany side @Transient. Best is to use cascade features of JPA. In your example, if you put:

@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval=true)
private List<Position> positions;

All operations on Order will cascade on Positions aswell, so you don't need to explicitly manage them.

See these examples with Hibernate

isah
  • 5,221
  • 3
  • 26
  • 36
  • Could I additionally also remove the `@ManyToOne`annotation for the order in `Position` with this? So I don't have to set this in each position before saving an order. – Semaphor Feb 18 '16 at 09:34
  • No, that is required in order to map the foreign key. Check this question on how to use cascade. http://stackoverflow.com/questions/3197667/how-to-cascade-persist-using-jpa-eclipselink – isah Feb 18 '16 at 11:27
  • I would also remove the member `order` from `Position` not only the annotation itself. – Semaphor Feb 18 '16 at 13:48
  • It's not making sense what you're trying to achieve. If there's a foreign key order_id on Position, then you shouldn't remove the member order and the @ManyToOne, because that's how you represent the relationship. – isah Feb 18 '16 at 15:15
  • Well I see have gain more basic unterstanding of this. Is the foreign key not automatically added but I just don't have any reference in my java object? Because my application don't need the reference in the `Position` class. – Semaphor Feb 18 '16 at 15:20
  • If `Order` is in a one-to-many relationship with `Position` then yes you need `@ManyToOne` in `order` reference in `Position` class. You should know your model. – isah Feb 18 '16 at 15:33
  • Maybe my question in the comments was unclear, I want to implement a unidirectional mapping what should be possible according to my reasearch so far. But anyway, you brought me to the right direction, thank you! – Semaphor Feb 21 '16 at 08:55