user_apr
user_apr

Reputation: 747

In Spring mvc How to add add set values to mysql

My goal :

In Spring MVC I have to save mobile phone contact list into database. example:

       phone1  sonia 2554654 work
                     2554654  home

multiple phone_number with multiple phone_Number type

contacts table

id,
contact_name
phone_number
phone_type

in my java class I have

public class ContactMobile {

  private String type;
  private String number;

  public ContactMobile() {
  }

  public ContactMobile(String type, String number) {
    super();
    this.type = type;
    this.number = number;
}

public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public String getNumber() {
    return number;
  }

  public void setNumber(String number) {
    this.number = number;
  }

}

and here I use SET for phone number and type

 @Entity
@Table(name = "_contact")
public class MobileContact {

  private String id;
  private String             fullname;
  private Set<ContactMobile> mobileNumbers;



  public MobileContact(String fullname, Set<ContactMobile> mobileNumbers) {
    super();
    this.fullname = fullname;
    this.mobileNumbers = mobileNumbers;
}

@Id
@Column(name = "Id")
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@Column(name = "fullname")
public String getFullname() {
    return fullname;
  }

  public void setFullname(String fullname) {
    this.fullname = fullname;
  }


  public Set<ContactMobile> getMobileNumbers() {
    return mobileNumbers;
  }

  public void setMobileNumbers(Set<ContactMobile> mobileNumbers) {
    this.mobileNumbers = mobileNumbers;
  }

public MobileContact() {
    super();
}

}

I am using hibernate to store data.. my question is in my MobileContact class in

public Set<ContactMobile> getMobileNumbers() {
        return mobileNumbers;
      }

what annotation I have to use here to save multiple phonenumbers?

Upvotes: 0

Views: 176

Answers (2)

Ralph
Ralph

Reputation: 120811

You can use the Embeddables (instead of Entities) for very simple value objects like MobileContact (then they do not need an ID, and the are no just simple value objects without own identity)

@Embeddable
public class ContactMobile {... 
    //implement an equals and hashcode method!
}


public class MobileContact {
    ...

    @ElementCollection
    private Set<ContactMobile> mobileNumbers;
    ...
} 

@See Java Persistence/ElementCollection

Upvotes: 0

Mavlarn
Mavlarn

Reputation: 3883

The MobileContact entity has many ContactMobile, it is a OneToMany relation. In your ContactMobile table, you should has a field for the id of MobileContact, like mobile_contact_id, and set the join column on that field as below in your ContactMobile:

@OneToMany(fetch = FetchType.LEZY)
@JoinColumn(name = "mobile_contact_id")
private Set<ContactMobile> mobileNumbers;

You can get the detail about the relation in this.

Upvotes: 1

Related Questions