0

I am developing my first application using JPA. There I have a requirement that the parent object has a list of child objects and that list is not separately entered to its table.

Here are my Entities.

Tender :-

@Entity
@Table(name = "tender")
public class Tender {

    @Id
    @Column(name = "tender_id", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "name")
    private String name;

    @Column(name = "description")
    private String description;

    // what are the annotations here?
    private List<TenderAddress> addresses;

    // constructors

    // getters ans setters

}

TenderAddress :-

@Entity
@Table(name = "tender_address")
public class TenderAddress {

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @GeneratedValue
    private long id;

    // what are the annotations here?
    private Tender tender;

    @Column(name = "address", nullable = false)
    private String address;

    // constructors 

    //getters and setters

}

I set addresses to the tender object using the setter method of the tender. This is how I save the Tender objects to the database. I use entity manager for that.

@Transactional
public Tender saveNewTender(Tender tender) {            
        entityManager.persist(tender);

        entityManager.flush();

        return tender;
}

What I need is to save the Address entity at the same time and the address table to have a reference to the tender object which encapsulates the particular address.

I tried with bidirectional one to many mapping using @JoinColumn and @OneToMany(mappedBy="tender", cascade = CascadeType.ALL). But it marked the reference column to NULL(tender id field in the Address table)

Now my question is, is my requirement valid? Do I have to persist the tender object first and take the id of it and then persist the Address object?

If my requirement is valid, how can I get it implemented? What can you recommend me? Please provide me the annotations I need in my entities.

vigamage
  • 1,975
  • 7
  • 48
  • 74
  • "What are the annotations here?" seriously? There are many internet references for JPA N-1 relations, and all say to use @ManyToOne (with optional cascade if you want to cascade). – Neil Stockton Feb 08 '17 at 08:47
  • I suggest to read the JPA guide or DOC. The most basic concepts are all there and after an hour, this is done. As @NeilStockton said, it's the basic and we may give answers but it's not the way you study it. – WesternGun Feb 08 '17 at 08:54
  • Thank you for your comments. But As I have mentioned in my post, once I tried with `@JoinColumn` and `@OneToMany(mappedBy="tender", cascade = CascadeType.ALL)`. And the result is also mentioned. Then I used `@IndexColumn`, it resulted with no default value error. Then I tried with `@orderColumn`. It also resulted with No default vale error. – vigamage Feb 08 '17 at 08:59
  • @NeilStockton I tried what u have suggested. What I came up with also mentioned in the question. All the references I found is something like first save the parent, then save the children with parent's id. That is not what I am looking for. – vigamage Feb 08 '17 at 09:00
  • @FaithReaper I asked a question just like what you have instructed me to ask for this same purpose. But I got only one answer and that was not helpfull. That is why I asked this question with most basic need I have. THis is it. http://stackoverflow.com/questions/42085823/filed-some-field-doesnt-have-a-default-value-jpa-mapping-one-to-many-relati – vigamage Feb 08 '17 at 09:04
  • http://www.datanucleus.org/products/accessplatform_5_0/jpa/orm/one_to_many_collection.html#fk_bi – Neil Stockton Feb 08 '17 at 09:13
  • @NeilStockton this sets the foreign key to NULL. That is why I asked is my requirement valid. I do not save the tender and the address objects separately. Tender object contains address objects, I need those address objects to be saved when the tender object is saved to the db. – vigamage Feb 08 '17 at 09:29
  • If you put annotations on as per that doc, and have cascade set then a COMPLIANT JPA provider will persist both. – Neil Stockton Feb 08 '17 at 09:30
  • @NeilStockton That is exactly what I am struggling to understand here. According to the tutorials I read if i have set `cascade = CascadeType.ALL`, when a parent is saved, all its children must be saved. Yes it saves the children, but the foreign key is null. That is the problem at the moment – vigamage Feb 08 '17 at 09:38
  • 1
    and you set BOTH SIDES of the relation? because your "question" omits such things ... – Neil Stockton Feb 08 '17 at 09:43
  • @NeilStockton As I thought it is needed only in the parent side. isn't it? – vigamage Feb 08 '17 at 10:08
  • needed at BOTH sides – Neil Stockton Feb 08 '17 at 10:13
  • `@GeneratedValue(strategy = GenerationType.AUTO)` set this on `TenderAddress`? – WesternGun Feb 08 '17 at 10:59

0 Answers0