How to save a foreign key in hibernate?

aratata

I have a timesheet entity that has a list of tasks. When I try to add a new task to the timesheet everything gets saved except the foreign key. Any ideas how should I avoid making a method with a custom SQL query and do the job in hibernate fashion?

My timesheet entity:

@Entity
@Table(name = "timesheet")
public class Timesheet {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private LocalDate date;

    @OneToMany(
            mappedBy = "timesheet",
            cascade = CascadeType.ALL,
            orphanRemoval = true,
            fetch = FetchType.EAGER
    )
    @JsonManagedReference
    private List<Task> tasks = new ArrayList<>(); 

My task:

@Entity
@Table(name = "task")
public class Task {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String title;

    private Integer hours;

    @ManyToOne
    @JsonBackReference
    private Timesheet timesheet; 

Service impl:

 @Override
    public void addTask(int timesheetId, TaskVO taskVO) {
        Timesheet oldTimesheet = timesheetRepository.findById(timesheetId).orElse(new Timesheet(date, tasks));
        Task task = taskMapper.transformToEntity(taskVO);
        taskRepository.save(task);
        oldTimesheet.getTasks().add(task);
        timesheetRepository.save(oldTimesheet);
    }
M A

There is a missing link from the Task object to its owner Timesheet. Filling this relationship should persist the foreign key column in the Task table, otherwise the JPA persister will still view the timesheet field as null in the Task object:

@Override
public void addTask(int timesheetId, TaskVO taskVO) {
    Timesheet oldTimesheet = timesheetRepository.findById(timesheetId).orElse(new Timesheet(date, tasks));
    Task task = taskMapper.transformToEntity(taskVO);
    task.setTimesheet(oldTimesheet);   // need this
    taskRepository.save(task);
    oldTimesheet.getTasks().add(task);
    timesheetRepository.save(oldTimesheet);
}

Note: I'm not sure about this, but you maybe don't need to save the task, if you are saving the timesheet at the end, since you have CascadeType.ALL on the tasks field.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to save a foreign key in hibernate?

From Dev

Save hibernate object with foreign key without loading the dependent object

From Dev

Hibernate save object (one to many relationship) foreign key is null

From Dev

Hibernate Foreign Key Error

From Dev

Hibernate not setting foreign key

From Dev

foreign key and mysql with hibernate

From Dev

Save object with Foreign key

From Dev

How to use a Primary Key also as a Foreign Key reference with JPA and Hibernate?

From Dev

Hibernate Save only Child table entry with parent foreign Key in Composite primary key

From Dev

how to update foreign key value in child table using hibernate

From Dev

How does hibernate generate foreign key constraint names?

From Dev

How can foreign key and reference class live together in a Hibernate entity?

From Dev

How to set null to foreign key column in OneToMany relationship, Hibernate

From Dev

Hibernate Criteria: How to retrieve table data with foreign key relationship

From Dev

jpa / hibernate how to map element collection by foreign key with annotations

From Dev

How to populate foreign key in table Spring + Hibernate + Spring Security

From Dev

How do I disable Hibernate foreign key constraint on a bidirectional association?

From Dev

How to define the order of fields on foreign key mapping using Hibernate and JPA?

From Dev

How to use two foreign keys as primary key on Hibernate entity annotation

From Dev

How to set null to foreign key column in OneToMany relationship, Hibernate

From Dev

How to match a hibernate/JPA table without a foreign key

From Dev

DRF how to save a model with a foreign key to the User object

From Dev

Django. How to get session and save it as a Foreign Key.

From Dev

Symfony - How to save foreign key in form "many-to-one"

From Dev

How to Save string property with foreign key reference in database

From Dev

How to save Foreign Key text input in Django model form

From Dev

mvc 5 code first userid as a Foreign Key - How to save data

From Dev

Hibernate: insert data with foreign key

From Dev

Creating a foreign key mapping in hibernate

Related Related

  1. 1

    How to save a foreign key in hibernate?

  2. 2

    Save hibernate object with foreign key without loading the dependent object

  3. 3

    Hibernate save object (one to many relationship) foreign key is null

  4. 4

    Hibernate Foreign Key Error

  5. 5

    Hibernate not setting foreign key

  6. 6

    foreign key and mysql with hibernate

  7. 7

    Save object with Foreign key

  8. 8

    How to use a Primary Key also as a Foreign Key reference with JPA and Hibernate?

  9. 9

    Hibernate Save only Child table entry with parent foreign Key in Composite primary key

  10. 10

    how to update foreign key value in child table using hibernate

  11. 11

    How does hibernate generate foreign key constraint names?

  12. 12

    How can foreign key and reference class live together in a Hibernate entity?

  13. 13

    How to set null to foreign key column in OneToMany relationship, Hibernate

  14. 14

    Hibernate Criteria: How to retrieve table data with foreign key relationship

  15. 15

    jpa / hibernate how to map element collection by foreign key with annotations

  16. 16

    How to populate foreign key in table Spring + Hibernate + Spring Security

  17. 17

    How do I disable Hibernate foreign key constraint on a bidirectional association?

  18. 18

    How to define the order of fields on foreign key mapping using Hibernate and JPA?

  19. 19

    How to use two foreign keys as primary key on Hibernate entity annotation

  20. 20

    How to set null to foreign key column in OneToMany relationship, Hibernate

  21. 21

    How to match a hibernate/JPA table without a foreign key

  22. 22

    DRF how to save a model with a foreign key to the User object

  23. 23

    Django. How to get session and save it as a Foreign Key.

  24. 24

    Symfony - How to save foreign key in form "many-to-one"

  25. 25

    How to Save string property with foreign key reference in database

  26. 26

    How to save Foreign Key text input in Django model form

  27. 27

    mvc 5 code first userid as a Foreign Key - How to save data

  28. 28

    Hibernate: insert data with foreign key

  29. 29

    Creating a foreign key mapping in hibernate

HotTag

Archive