Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How to map an association with an Enum
How to map an association with an Enum [message #530788] Fri, 30 April 2010 18:15 Go to next message
Chris Mathrusse is currently offline Chris Mathrusse
Messages: 24
Registered: July 2009
Junior Member
Using EclipseLink 2.0.1, JDK 6

I have a challenging situation that I have inherited. Currently this is being done with Hibernate but I've gotten the approval to migrate over to EclipseLink.

I have a table, Address

CREATE
  TABLE Address
  (
    AddressID ID NOT NULL IDENTITY
, ... some more fields
, AddressTypeID INT
)


And table AddressType
CREATE 
  TABLE AddressType 
(
  id NOT NULL int
, name varchar(30)
, ref varchar(30)
, isActive bit
)


The challenge I am faced with is that the AddressType table is actually a view. The id field is the actual primary key in the table that the view is build upon. The ref field is a String which represents the actual type of data the view represents (Billing,Shipping,US,CustomerService,etc..). When an association is made to the Address table the AddressType.id is stored on the Address record.

What I would like to do is have a single enum class, AddressType, that represents the String values only and have a reference to the enum on the Address class. But how can I ensure during insert/update operations that the actual ID of the AddressType gets persisted? In addition, how can I map that ID from the Address table to the enum during a read operation?

All suggestions are welcome.
Thanks...
Re: How to map an association with an Enum [message #530800 is a reply to message #530788] Sat, 01 May 2010 01:50 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom Eugelink
Messages: 806
Registered: July 2009
Senior Member
> The challenge I am faced with is that the AddressType table is actually
> a view. The id field is the actual primary key in the table that the
> view is build upon. The ref field is a String which represents the
> actual type of data the view represents

From my experience you basically can treat a view as a table, define an @Id on the view and create a many-to-one relation between the two. Only persisting and merging the entities is quite useless.

Maybe that can be combined with this code that turns a table into an enum: http://wiki.eclipse.org/EclipseLink/Examples/JPA/EnumListMap ping#Using_EclipseLink_.40BasicCollection

The biggest drawback would be that you need to explicitly define the enum in Java, so you lose the dynamics of it being a table somewhere behind the view and need to recompile if a enum value is added. Won't a simple many-to-one be just as efficient?

Tom
Re: How to map an association with an Enum [message #531126 is a reply to message #530800] Mon, 03 May 2010 15:44 Go to previous messageGo to next message
Chris Mathrusse is currently offline Chris Mathrusse
Messages: 24
Registered: July 2009
Junior Member
Thanks for the suggestion. I've used @Enumerated(EnumType.STRING) for a PrimaryKey before on another project without any issue, and I know that you loose the flexibility of the dynamic nature of adding new values. But if the data is extremely static then I'm willing to live with that trade-off.

The issue I have now is a bit different though. Given my table definition from above, the AddressType view returns data as follows:


address_type_id 	      value
120503	                Billing
120507		        CustomerService
120501		        Normal
120508		        Other
120506		        SendTicketsTo
120502		        Shipping
120504		        Site
120509		        SubOffice
120505		        US


The Address table stores the address_type_id, an int value, on the record and not the string value. I don't want to code up the enum to use the int values as these are pseudo keys and can change from one database to another.

So I think my only solution here is with some sort of object mapper, mapping the AddressType object that is maintained by JPA to a simply Java Enum type. Of course this mapping would need to occur during load, insert and update. So maybe by utilizing the @PreUpdate, @PrePersist annotations and managing the relationship within the code. Normally this would be trivial but I can't figure out an easy way to gain access to the EntityManager from within a mapping class.

Can someone tell me if I'm heading down the right path or if there is a better, easier way to accomplish this?

Thanks for the help....
Re: How to map an association with an Enum [message #531175 is a reply to message #531126] Tue, 04 May 2010 01:19 Go to previous message
Tom Eugelink is currently offline Tom Eugelink
Messages: 806
Registered: July 2009
Senior Member
> Normally this
> would be trivial but I can't figure out an easy way to gain access to
> the EntityManager from within a mapping class.

For my projects I also wanted any entity to be able to access an EntityManager in order to add or modify other entities. So I introduced the concept of EntityManagerFinder.

You can do a EntityManagerFinder.find() and it will return the correct EM. The EMF can either find the EM by GUI component (one EM per JFrame for fat clients) or by thread (for webapplications). Naturally somewhere there is code binding an EM to these, e.g. in a webapp there is a filter that allocates an EM and binds it to the thread being assigned to process the request.

That might solve this problem.
Previous Topic:left outer join with JPQL
Next Topic:Problem installing EclipseLink 2.0.2 in Eclipse 3.5/Galileo
Goto Forum:
  


Current Time: Tue Jul 22 07:31:53 EDT 2014

Powered by FUDForum. Page generated in 0.11451 seconds