0

So, I understand the question seems a little odd and it probably is but to clarify:

@Entity
@Table(name = "OUTTER")
class Outter(){

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "outterId")
    var id: Long? = null

    @ElementCollection
    val listOfInner = mutableListOf<Inner>()
}
@Embeddable
@Table(name = "INNER")
data class Inner(
        @Id
        @JoinColumn(name = "outterId")
        var outter: Outter
)

doesn't work and I'm not sure why. It seems to follow the guides I've seen so not sure what I'm missing.

OUTTER:

outterId | outterStuff 
---------------------
 1       | info      

INNER:

outterId | innerIndex | innerStuff
--------------------------------
 1       | 0          | innerInfo

so it's 1 to multiple but the primary key for inner is outterId and then it uses other keys to make sure that it is unique (not an embedded ID tho)

Alex
  • 419
  • 6
  • 24

1 Answers1

0

Embeddable's don't have an independent identity. Neither should you define a @Table for them.

@Entity
@Table(name = "OUTTER")
class Outter(){

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "outterId")
    var id: Long? = null

    @ElementCollection
    @CollectionTable(name = "INNER", joinColumn = "outterId")
    val listOfInner = mutableListOf<Inner>()
}

@Embeddable
data class Inner(
        //fields as required
)
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • okay but what if they're already in another table? Should I then think of another way to do it? – Alex Dec 04 '19 at 19:34
  • What do you mean by *already in another table*? I am not really sure what you are asking? Maybe you should add the table structure(s) to your question as it is not very clear? – Alan Hay Dec 04 '19 at 19:47
  • added the tables – Alex Dec 04 '19 at 20:48
  • The mappings I have given will work. I am not sure why you think they won't. Try them! – Alan Hay Dec 04 '19 at 22:21
  • Get an error Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'innerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: The given domain class does not contain an id attribute! – Alex Dec 04 '19 at 23:00
  • Because you have made it an embeddable rather than an entity. I think you need to read up on some basic concepts. https://stackoverflow.com/questions/21143707/what-is-difference-between-entity-and-embeddable – Alan Hay Dec 05 '19 at 07:48