5

I have two class and I want to use OneToMany relation with EmbeddedId (Im working with kundera framework) my sensor entity class:

public class SensorEntitie implements Serializable {
    @EmbeddedId
    private CompoundKey key;
    @Column
    private float temperature;
    @Column
    private float pressure;

    @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
    @JoinColumn(name="what I should to put here")
    private List<PieceEntitie> pieces;
}
@Embeddable
    public class CompoundKey
    {
        @Column 
        private String IdSensor;           
        @Column 
        private long date;           
        @Column(name = "event_time")
        private long eventTime;

my piece class entity

public class PieceEntitie implements Serializable{

    /**
     * 
     */
    @Id
    private String IdPiece;
    @Column
    private double width;
    @Column
    private double height;
    @Column
    private double depth;

but how can i fill the blank in @JoinColumn

h.zak
  • 1,407
  • 5
  • 21
  • 40

2 Answers2

11

I found the solution : to use OneToMany relation with EmbeddedId, I should to declare JoinColumns and multiple of JoinColumn

@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
    @JoinColumns({
        @JoinColumn(name = "idsensor", referencedColumnName = "idsensor"),
        @JoinColumn(name = "date", referencedColumnName = "date"),
        @JoinColumn(name = "event_time", referencedColumnName = "event_time")
})
h.zak
  • 1,407
  • 5
  • 21
  • 40
  • While in general mappedBy will work, there are circumstances where `@JoinColumns` is needed, such as where a composite key in one table is part of a primary key in another in a `@ManyToOne`. It's a shame that JPA doesn't have support for that (or if it does I've not found that magic). – NealeU Oct 13 '17 at 09:02
  • 1
    `@OneToMany`? with a `@JoinColumns` ? – lealceldeiro Sep 14 '22 at 12:29
  • @lealceldeiro, he uses [unidirectional one-to-many association](https://docs.oracle.com/javaee/6/api/javax/persistence/JoinColumn.html). But it's not efficient. [The best way to map a `@OneToMany` relationship with JPA and Hibernate](https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/) – Arash May 30 '23 at 09:07
2

You need to do some following steps for fixing problem

  1. Remove @JoinColumn you dont need to write that statement
  2. Remove @OneToMany to created object
  3. Bind @OneToMany with getter method as per my following code
 @OneToMany(mappedBy = "pieceEntitie", cascade = CascadeType.ALL, 
 fetch=FetchType.EAGER)
    public Set<PieceEntitie> getPieceEntitie() {
            return pieceEntitie; 
    }
Ravi Kavaiya
  • 829
  • 6
  • 16
  • I tried your solution, and I fixed some problems, but this doesn't change anything, my framework saves data into separate tables without any relationship between two tables, so I can't get for example all data which are associated to my sensor table – h.zak May 11 '16 at 13:59