When trying to insert an Entity - A
with a set
Another Entity B
, B
should get the Auto generated Id
from A
but its null
.
Tried and failed:
@MapsId("taskPKId.storyId.id")
- Same error.@Embeddable class StoryId { @ManyToOne(fetch = FetchType.Lazy) @JoinColumn(name = "STORY_ID") Long id; }
//Incomprehensible Null pointer exceptionmappedBy("story")
- same error- Tried with mappedBy('story') but getting an error with repeated column and so had to map it with
insertable=false
andupdatable=false
[Hibernate doesn't recognizeinsertable=false
for@EmbeddedId
]
- Tried with mappedBy('story') but getting an error with repeated column and so had to map it with
I am getting STORY_ID = null
and therefore saveAll
fails on storyRepository.saveAll(stories)
where storyRepository
is a Spring Data repository
@Table(name = "STORY")
@EqualsAndHashCode
class Story {
@Id
@GeneratedValue(stratergy=GenerationType.Auto)
@Column(name="STORY_ID")
Long id;
@Column(name="STORY_NAME")
String name;
//@OneToMany(cascade=ALL, mappedBy="taskPKId.storyId.id", fetch = FetchType.Lazy) // tried this as well
@OneToMany(cascade=ALL, mappedBy="story", fetch = FetchType.Lazy)
Set<Task> task;
}
@Table(name = "TASK_XREF")
@EqualsAndHashCode
Class Task {
@EmbeddedId
TaskPKId taskPKId;
@Column(name = "TASK_NAME")
String name;
@ManyToOne (fetch = FetchType.Lazy, optional = false)
@JoinColumn(name = "STORY_ID", referencedColumnName = "STORY_ID", nullable = false, insertable = false, updatable = false)
Story story;
}
@Embeddable
@EqualsAndHashCode
Class TaskPKId implements Serializable {
TaskId taskId;
TaskTypeId taskTypeId;
StoryId storyId;
}
@Embeddable
@EqualsAndHashCode
class StoryId implements Serializable {
@Column(name = "STORY_ID")
Long id;
}
Tables:
- STORY [
STORY_ID
,STORY_NAME
] - TASK_XREF [
(TASK_ID(FK), TASK_TYPE_ID(FK), STORY_ID(FK))
PK,TASK_NAME
]
Story gets inserted (before commit ofcourse), but fails because STORY_ID
is sent as null to TASK_XREF
for the next inserts