0

There is simple model of associated entities:

MainModel class

@Entity 
@Table(name = "main_model")
public class MainModel 
{
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private long id;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "id_main_model", referencedColumnName = "id")
    private List<AssociatedModel> am_collection;

    public MainModel() { }

    public long getId()
    {
        return this.id;
    }

    public void addAssociated(AssociatedModel am)
    {
        am_collection.add(am);
    }
}

AssociatedModel class

@Entity 
@Table(name = "associated_model")
public class AssociatedModel
{
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private long id;

    public AssociatedModel() { }

    public long getId()
    {
        return this.id;
    }
}

Simplest jpa repository for MainModel

public interface MainModelDAO extends JpaRepository<MainModel, Long>  
{
}

and finally, controller's method for creating and saving AssociatedModel instance

@RequestMapping("/url")
public String createAssociated(@RequestParam("id_mainmodel") long id_mainmodel, @RequestBody AssociatedModel newAm) 
{
    MainModel mm = MainModelDAOobject.findOne(id_mainmodel);// MainModelDAOobject - @Autowired
    mm.addAssociated(newAm);
    MainModelDAOobject.saveAndFlush(mm);
    return String.valueOf(newAm.getId());// get Id of associated saved object
} 

Associated obect saves into database correctly, but id value of this object, allowed by its getId() method, always zero. How can I get Id of the saved associated object correctly?

gorill
  • 1,623
  • 3
  • 20
  • 29

3 Answers3

1

Try calling MainModelDAOobject.flush() after saving the value, but before reading the ID.

See this answer for an explanation.

Edit: try manually saving newAm by adding a new line:

mm.addAssociated(newAm); // Existing line
newAm = MainModelDAOobject.save(newAm); // New line
MainModelDAOobject.save(mm); // Existing line
Community
  • 1
  • 1
David Levesque
  • 22,181
  • 8
  • 67
  • 82
0

you need a sequencegenerator:

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MainModel SequenceGenerator")
@SequenceGenerator(name = "MainModel SequenceGenerator", sequenceName = "MainModel Sequence", initialValue = SEQUENCE_INITIAL_VALUE, allocationSize = SEQUENCE_ALLOCATION_SIZE)
Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
  • Ids for the created records generates correctly in the database. I just cant get them after save. – gorill Jan 29 '14 at 16:11
-1

Thanks to David Levesque for helpful suggestion.

problem is solved as follows:

  1. need additional repository for AssotiatedModel

    public interface AssotiatedModelDAO extends JpaRepository {}

  2. then just save associated object manually:

    mm.addAssociated(newAm);

    newAm = AssotiatedModelDAOobject.save(newAm);

    newAm = MainModelDAOobject.save(newAm)`;

    MainModelDAOobject.save(mm);

gorill
  • 1,623
  • 3
  • 20
  • 29