1

How to store following the entity using Hibernate?

@Entity
class A {
  private Map<String, String> b;

  // getters and setters omitted
}
ntviet18
  • 752
  • 1
  • 10
  • 26

3 Answers3

0

Have a look at @ElementCollection

Example usage:

@Entity
public class User {

   public String getLastname() { ...}

   @ElementCollection
   @CollectionTable(name="Nicknames", joinColumns=@JoinColumn(name="user_id"))
   @Column(name="nickname")
   public Set<String> getNicknames() { ... } 
}
Diyarbakir
  • 1,999
  • 18
  • 36
0

Using Save

Here an example:

A variable = new A();
variable.b(your_variable);

Then

session.save(varible);

can be used. Or you mean to store in a database by the storage? Then will be so:

  SessionFactory factory=cfg.buildSessionFactory();   
  Session session=factory.openSession();  
  Transaction t=session.beginTransaction();   
  A e1=new A();  
  e1.setb(your_variable);  
  session.persist(e1);
  t.commit();
  session.close(); 

Mention: your naming is quite bad. You should put something else!

Also, with the @ElementCollection annotation you can use java.util.Map collections. In the declaration of your class (in your case, A).

Andreea Craciun
  • 302
  • 1
  • 6
0

It seems like you should use @ElementCollection and @CollectionTable from JPA: How to annotate Map<Entity,INTEGER> with JPA?

Zufar Muhamadeev
  • 3,085
  • 5
  • 23
  • 51