-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Map a HashMap using annotations instead of xml
PostPosted: Fri Jan 14, 2011 8:22 am 
Newbie

Joined: Fri Jan 14, 2011 6:51 am
Posts: 2
This code maps a HashMap but I would like to do the same using annotations. Is it possible? Clould you help me?
Thaks in advance!!!

CrmCliente.java

public class CrmCliente extends PlmEntity implements IActionProvider, IORMBasicMetadataProvider {

private static IORMBasicMetadata metadata;
private String nombre;
private HashMap userCustomField = new HashMap();}


CrmCliente.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 22-jun-2010 17:12:58 by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
<joined-subclass
name="com.divisoft.modules.crm.shared.data.mapping.db.hibernate.entity.CrmCliente"
table="Crm_Cliente"
extends="com.divisoft.lib.prolib.shared.data.mapping.db.hibernate.entity.PlmEntity">
<key column="id_cliente" />
<property name="nombre" type="string">
<column name="nombre"/>
</property>
<one-to-one name="userCustomField" entity-name="com.divisoft.modules.crm.shared.data.mapping.db.hibernate.entity.CrmClienteCustomUserProperties"
embed-xml="true" cascade="delete,all">
</one-to-one>

</joined-subclass>
</hibernate-mapping>

CrmClienteAux.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 28-abr-2010 10:34:06 by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
<class entity-name="com.divisoft.modules.crm.shared.data.mapping.db.hibernate.entity.CrmClienteCustomUserProperties" table="Crm_ClienteAux">
<id name="id" type="int">
<column name="id" />
<generator class="foreign">
<param name="property">entidad</param>
</generator>
</id>
<one-to-one name="entidad"
class="com.divisoft.modules.crm.shared.data.mapping.db.hibernate.entity.CrmCliente"
constrained="true" cascade="all">
</one-to-one>

<property name="direccion" type="string">
<column name="direccion" />
</property>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Map a HashMap using annotations instead of xml
PostPosted: Fri Jan 14, 2011 8:37 am 
Newbie

Joined: Thu Jan 13, 2011 3:55 pm
Posts: 4
maity_bi wrote:
This code maps a HashMap but I would like to do the same using annotations. Is it possible? Clould you help me?
Thaks in advance!!!


You cannot use directly HashMap, but you can use Map:

Code:
//imports
@Entity
@Table(name="Person")
public class Person {

   private String name;
   private int id;
   private Map<String,Phone> phones;

   public Person(){}

   @Column(name="Nome")
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }

   @Id
   @GeneratedValue(strategy= GenerationType.AUTO)
   @Column(name="id")
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }

//a Person has many Phones --> one-to-many relationship
   @OneToMany
   public Map<String, Phone> getPhones() {
      return telefoni;
   }

}

This is a basic way to use JPA annotations for Maps, and this kind of annotation triggers the creation of a JoinTable. It works for List and Set too.


Top
 Profile  
 
 Post subject: Re: Map a HashMap using annotations instead of xml
PostPosted: Fri Jan 14, 2011 9:06 am 
Newbie

Joined: Fri Jan 14, 2011 6:51 am
Posts: 2
The problem is that the HashMap has a non defined object (we don't have any java class). So "Crm_ClienteAux" table is dynamically created depending on object.
How can I do this with annotations?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.