Malinda
Malinda

Reputation: 584

How to convert following hibernate annotaion to JPA

 <map name="urls" lazy="false">
        <cache usage="nonstrict-read-write"/>
        <key column="EMP_ID"/>
        <map-key column="EMP_URL_TYPE_ID" type="int"/>
        <one-to-many class="com.employee.ems.EMSURL"/>
 </map>

where Primary Key of EMP Table is "EMP_ID" and

Upvotes: 1

Views: 123

Answers (2)

Malinda
Malinda

Reputation: 584

In EMP entity:

 @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
 @OneToMany(fetch=FetchType.EAGER)
 @JoinColumn(name="EMP_ID")
 @MapKey(name="empUrlType")
 private Map<Integer,EMSURL> urls

In EMSURL entity:

  @JoinColumn(name="EMP_URL_TYPE_ID")
  private String empUrlType;

Upvotes: 0

JLazar0
JLazar0

Reputation: 1292

Try this:

In EMP entity:

@OneToMany(mappedBy="emp",fetch=FetchType.EAGER)
@MapKey(name="empUrlTypeId")
private Map<Integer,EMSURL> urls

In EMSURL entity:

@Id
private Integer empUrlTypeId;

@ManyToOne
@JoinColumn(name="EMP_ID")
private EMP emp;

name = "urls" corresponds to the name of the property of the entity where the collection is defined, lazy false implies that the relationship is defined as EAGER, as oneToMany is the PK is in the target entity, so we define the relationship in the EMSURL entity and in EMS we establish a bidirectional relationship through mapping, the Integer type in the object is not mapped, since when modeling the JPA entities the name of the column is indicated in the @JoinColumn annotation but the type is that of the own entity.

Upvotes: 1

Related Questions