0

I have 2 legacy tables:

CREATE TABLE A (
    ID NUMBER PRIMARY KEY , 
    DATA NUMBER
)

CREATE TABLE A_CONF (
    A_ID NUMBER,    // FK to A
    INFO VARCHAR2(256)
)

Creating the JPA entity for A is straightforward. Yet, what can I do retrieve the multiple INFO fields that can be associated to an instance of A since there is no PK in A_CONF and therefore cannot create an entity for it?

Thanks for helping.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118

2 Answers2

1

Seems like you are looking for what JPA calls an "element collection":

@Entity
public class A {
    @Id
    private Long id;
    private Long data;

    @ElementCollection
    @CollectionTable(name="A_CONF", joinColumns=@JoinColumn(name="A_ID")) // A_ID would be the default join column
    @Column(name="INFO")
    private Set<String> infos; // using Set assuming unique values
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
Steve Ebersole
  • 9,339
  • 2
  • 48
  • 46
  • The problem is that I can have several ``String data``! – Jean Logeart Jun 01 '12 at 14:03
  • So then add your several String fields. We can only answer based on information you actually give in your question... – Steve Ebersole Jun 01 '12 at 21:58
  • I did: "what can I do retrieve the multiple INFO fields that can be associated to an instance of A". What do you mean by "add your several String fields"? – Jean Logeart Jun 02 '12 at 10:23
  • Well you never said what "multiple INFO fields" mean. To me "multiple fields" means multiple fields (field1, field2, ...) on the Java class. Obviously that means something else to you. Also, how is this in any conceivable way an entity? So now that you have clarified a bit, I am guessing you are looking for @ElementCollection. See my updated answer. See it helps to be precise in your questions :) – Steve Ebersole Jun 03 '12 at 14:02
0

You can define a primary key in your model class even if your table doesn't have one, just pick one or some columns in your model and put them as ids.

Euclides Mulémbwè
  • 1,677
  • 11
  • 18