How to define the EclipseLink annotation for the f

2019-07-14 20:05发布

问题:

I'm relativly new to the world of EclipseLink, I've been reading through the documentation, however I'm having a real problem trying to represent the following table.

PTY_NO  |   REF_OBG
6544        45663
6544        1234
6544        97543
6544        1123
6544        77897

Ideally I'd like to represent the above data as follows.

@Entity
@Table(name="FCS_ISSR_OBG")
public class fcs_issr_obg implements Serializable  {

    @Id
    @Column(name="PTY_NO")
    private long pty_no;

    @Column(name="REF_OBG")
    private List<long> ref_obg;

...

As once I have the data in this form I plan to serialize the class into a Coherence cache.

However the annotation I've used doesn't actually compile ...

Any help would be gratefully received.

.. update

The best I've managed to come up with so far is

@Entity
@Table(name="FCS_ISSR_OBG")
public class fcs_issr_obg implements Serializable, PortableObject {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="PTY_NO")
    private long pty_no;

    @ElementCollection(targetClass = Long.class, fetch = EAGER)
    @CollectionTable(
            name = "FCS_ISSR_OBG",
            joinColumns=@JoinColumn(name="PTY_NO")
            )
    @Column(name ="REF_OBG")
    private List<Long> collection;

However this results in 2 queries ... which is not really what I'm after.

Cheers Rich

回答1:

To force any relationship to be fetched with the parent query the @JoinFetch annotation can be used in EclipseLink.

Although, reading the collection in a separate query may be the best solution depending on the data.

You can also use @BatchFetch in EclipseLink to batch fetch a relationship (still 2 queries, but not n+1 queries). I did a comparison on batch and join fetching in my blog recently, see,

http://java-persistence-performance.blogspot.com/



回答2:

I've not been able to test this, but perhaps introducing an embeddable object may reduce the query count. Something like this:

@Entity
@Table(name = "FCS_ISSR_OBG")
public class FCS_ISSR_OBGDto implements Serializable {

  @Column(name = "PTY_NO", nullable = false)
  private Long pty_no;

  private List<REF_OBGDto> REF_OBGs = new ArrayList<REF_OBGDto>();

  @ElementCollection
  @CollectionTable(name = "FCS_ISSR_OBG", joinColumns = @JoinColumn(name = "PTY_NO"))
  @Column(name = "REF_OBG")
  public List<REF_OBGDto> getREF_OBGs() {
    return REF_OBGs;
  }
}

with the embeddable looking like this

@Embeddable
@Table(name = "FCS_ISSR_OBG")
public class REF_OBGDto {

  @Column(name = "REF_OBG")
  public Long ref_obg;

}

You only get the collection when you specifically ask for it. Sorry for the weak answer but I'm limited to what I can test here.