0

I have the following (JPA/Hibernate entities)

@Entity
@IdClass(MakeId.class)
public class Make {
    @Id
    @Column(name="MAKE_ID")
    private String makeUuid;

    @Id
    private String city

    @Id @Column(name = "MAKE_ID_VERSION")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "version_seq")
    @GenericGenerator(
            name = "version_seq", strategy = "com.me.VersionIdGenerator"
    )
    private String makeIdVersion;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumns( {
            @JoinColumn(name = "clientProgramUuid", referencedColumnName = "MAKE_ID"),
            @JoinColumn(name = "clientUuidVersion", referencedColumnName = "MAKE_ID_VERSION")
    })
    private List<MakeConfiguration> makeConfigurations;
}

@Entity
@IdClass(MakeConfigurationId.class)
public class MakeConfiguration {
    @Id
    private String makeConfigurationUuid

    @Id
    private String something

    @Id
    private String makeIdVersion;
}

IdClass, getters, setters omitted for readability. VersionIdGenerator generates the next version in sequence (let us say S_1, S_2...S_N)

My use case is as follows: Make -< many Make Configurations which share the Make_ID and the version.

When I get a specific Make (Id + Version) the correct MakeConfigurations are retrieved

My Desired Behavior:

Make 
ID   |   City   |   Vers
1        Jose        S_5

MakeConfiguration
MakeID | Something | Vers
1         nada       S_5

When I get this entity (using spring data jpa with some custom @Query) then everything works fine.

When I try to post (the exact same entity), I want the following:

Make 
ID   |   City   |   Vers
1        Jose        S_5
1        Jose        S_6

MakeConfiguration
MakeID | Something | Vers
1         nada       S_5
1         nada       S_6

But what I get instead is this:

Make 
ID   |   City   |   Vers
1        Jose        S_5
1        Jose        S_6

MakeConfiguration
MakeID | Something | Vers
1         nada       S_6

(Notice the lack of new record created in MakeConfiguration)

I have tried several things to get this to work (bidirectional, etc.) and with no luck. I think that I need to force the version to be retrieved from what is generated by the Make.

Draken
  • 3,134
  • 13
  • 34
  • 54
Dave L
  • 29
  • 1
  • 2
  • Did you put equals and hashcode methods in your entity? – ℛɑƒæĿᴿᴹᴿ Sep 06 '19 at 14:38
  • Yes, I have and the issue persists. When I set the cascade on the OneToMany mapping I get the following: BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1 – Dave L Sep 06 '19 at 15:04
  • Possible duplicate of [JPA force insert related entities (as opposed to update)](https://stackoverflow.com/questions/57826614/jpa-force-insert-related-entities-as-opposed-to-update) – Antonio112009 Sep 06 '19 at 20:33

0 Answers0