Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How to use @BasicMap for a HashMap whose key values are entities
How to use @BasicMap for a HashMap whose key values are entities [message #387366] Sun, 19 April 2009 05:40 Go to next message
No real name is currently offline No real nameFriend
Messages: 6
Registered: July 2009
Junior Member
Hi

I'm new to EclipseLink and I was wondering how should I use EclipseLink's
@BasicMap annotation for a map whose key value is an entity and the value
is
an Integer?

Example:


@Entity
class A {

// This doesn't work, as the key is an entity
@BasicMap
private Map<B, Integer> myMap = new HashMap<B, Integer>();

}

@Entity
class B {
...
}
Re: How to use @BasicMap for a HashMap whose key values are entities [message #387369 is a reply to message #387366] Mon, 20 April 2009 14:16 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

The @BasicMap annotation is mainly for when both the key and value are
Basic. However JPA 2.0 will have just extended Map support. If you use
the latest EclipseLink 2.0 build there should be some level of support for
the new @MapKeyClass, @MapKeyJoinColumn JPA 2.0 annotations.

Otherwise in EclipseLink 1.1 / JPA 1.0, you could map the relation table
as an Entity which contains the OneToOne to the key and the Basic value.

---
James
http://www.nabble.com/EclipseLink---Users-f26658.html


James : Wiki : Book : Blog : Twitter
Re: How to use @BasicMap for a HashMap whose key values are entities [message #387370 is a reply to message #387366] Mon, 20 April 2009 14:26 Go to previous messageGo to next message
Doug Clarke is currently offline Doug ClarkeFriend
Messages: 155
Registered: July 2009
Senior Member
BasicMap and BasicCollection support basic types as the keys and values.
As part of the JPA 2.0 work already completed in trunk for EclipseLink 2.0
the new javax.persistence.ElementCollection mapping allows mappings with
values being basic, embedables and if using a map the key can be basic,
embeddable, or and entity.

here is an example from the spec:

@Entity
public class VideoStore {
@Id int id;
String name;
Address location;
...
@ElementCollection
@CollectionTable(name="INVENTORY",
joinColumns=@JoinColumn(name="STORE"))
@Column(name="COPIES_IN_STOCK")
@MapKeyJoinColumn(name="MOVIE", referencedColumnName="ID")
Map<Movie, Integer> videoInventory;
...
}

This support is completed in EclipseLink trunk's nightly builds and future
milestone builds.

Doug
Re: How to use @BasicMap for a HashMap whose key values are entities [message #387373 is a reply to message #387370] Tue, 21 April 2009 20:35 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 6
Registered: July 2009
Junior Member
I tried what you suggested, but got this error message

Internal Exception: Exception [EclipseLink-7155] (Eclipse Persistence
Services - 2.0.0.r3652-M1):
org.eclipse.persistence.exceptions.ValidationException
Exception Description: The type [interface java.util.Map] for the
attribute [myMap] on the entity class [class MyAClass] is not a valid type
for a serialized mapping. The attribute type must implement the
Serializable interface.

The wiki page about error references was a bit cryptic

ECLIPSELINK-07155: The type [{1}] for the attribute [{0}] on the entity
class [{2}] is not a valid type for a serialized mapping. The attribute
type must implement the Serializable interface.
Cause: An annotation @AttributeOverride columns was not specified on a
mapping mapping from entity class ClassName.
Action: Specify this annotation.


Here what I have (yes, the key in the map is of same type as the class
itself):


@Entity
class MyAClass {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;

@CollectionTable(name = "MYACLASS_DEPENDENCIES", joinColumns =
@JoinColumn(name = "MYACLASS"))
@Column(name = "REQ_LEVEL")
@MapKeyJoinColumn(name = "MYACLASS", referencedColumnName = "ID")
private Map<MyAClass, Integer> myMap = new HashMap<MyAClass ,
Integer>();

}
Re: How to use @BasicMap for a HashMap whose key values are entities [message #387374 is a reply to message #387373] Wed, 22 April 2009 04:50 Go to previous messageGo to next message
Doug Clarke is currently offline Doug ClarkeFriend
Messages: 155
Registered: July 2009
Senior Member
The feature is not available in 2.0M1. It will be available, at least in
part, the M2 milestone.

The work is tracked in bug:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=241413

and on the wiki:
http://wiki.eclipse.org/EclipseLink/Development/JPA2.0/Exten ded_Map_support

Doug
Re: How to use @BasicMap for a HashMap whose key values are entities [message #387378 is a reply to message #387374] Thu, 23 April 2009 16:14 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 6
Registered: July 2009
Junior Member
I tried the code above with a nightly build (2.0.0.v20090423-r4028) and I
still get the same error message.
JPA - How to use AVG between Calendar dates [message #387379 is a reply to message #387366] Fri, 24 April 2009 23:25 Go to previous messageGo to next message
Kenneth Unpingco is currently offline Kenneth UnpingcoFriend
Messages: 4
Registered: July 2009
Junior Member
Hello,

I am new to EclipseLink and to JPA. I wanted to ask how should I use JPA's
AVG (average) for computing the average time total between two Calendar
objects (response.date.time subtracted to request.date.time)? The Calendar
dates are gotten from a Docstore table having a modDate column.

Example:
In my EJB entity bean called AddressProcess.java, here is a snippet of the
code:
@NamedQuery(name = "AddressProcess.getAverageTimeForAddressesByDate",
query = "select AVG(res.modDate.time.time - req.modDate.time.time) from
AddressProcess as o JOIN o.requestDoc as req JOIN o.responseDoc as res
WHERE o.modDate BETWEEN :startDate AND :endDate")

I ran the program but I got a run-time exception error:
Exception Description: Syntax error parsing the query
[AddressProcess.getAverageTimeForAddressesByDate: select AVG(res.modDate -
req.modDate) from AddressProcess as o JOIN o.requestDoc as req JOIN
o.responseDoc as res WHERE o.modDate BETWEEN :startDate AND :endDate],
line 1, column 23: syntax error at [-].

Any recommendations/advice is greatly appreciated.

Thank you,
Ken U.
Re: JPA - How to use AVG between Calendar dates [message #387424 is a reply to message #387379] Mon, 27 April 2009 14:38 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

The expression is not support by JPQL.

Either use a native SQL query, or use EclipseLink's Expression query API.

---
James
http://www.nabble.com/EclipseLink---Users-f26658.html


James : Wiki : Book : Blog : Twitter
Previous Topic:SDO in OSGi environment
Next Topic:2 same queries => different results?
Goto Forum:
  


Current Time: Mon Dec 22 14:56:12 GMT 2014

Powered by FUDForum. Page generated in 0.03825 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software