Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » Const Mapping(Jpa+Const Mapping (not enum))
Const Mapping [message #850720] Fri, 20 April 2012 05:04 Go to next message
Eclipse UserFriend
Hello!

I have 2 classes:

public class State
{
	public static final State FIVE_STATE = new State(5, "five state");
	
	public static final State ONE_STATE = new State(1, "one state");
	
	private int id;
	
	private String name;
	
	private State(int id, String name)
	{
		this.id = id;
		this.name = name;
		
	}
}


and

@Entity
@Table(name="photo")
public class Photo
{
	@Id
	private int id;
	
	private State state;
}


How can I do mapping field "state"?

In pure eclipselink I use ObjectTypeConverter, like this:

public class PhotoDesc extends ClassDescriptor
{
	public PhotoDesc()
	{
		{...}
		
		DirectToFieldMapping d2f = new DirectToFieldMapping();
		d2f.setAttributeName("state");
		d2f.setFieldName("stateField");
		ObjectTypeConverter objectTypeConverter = new ObjectTypeConverter();
		
		objectTypeConverter.addConversionValue(State.FIVE_STATE.getId(), State.FIVE_STATE);
		objectTypeConverter.addConversionValue(State.ONE_STATE .getId(), State.ONE_STATE);
		
		d2f.setConverter(objectTypeConverter);
		
		{...}
	}
}


What annotation I can use in JPA?

[Updated on: Mon, 23 April 2012 00:18] by Moderator

Re: Const Mapping [message #854101 is a reply to message #850720] Mon, 23 April 2012 13:08 Go to previous messageGo to next message
Eclipse UserFriend
You can define the same converter in JPA by using a DescriptorCustomizer. Or you could define your converter as a subclass of ObjectTypeConverter and add the conversion values in its constructor and specifiy it using @Converter.

You may also consider make State an enum instead of a class and using JPA's Enum support.
Re: Const Mapping [message #855291 is a reply to message #854101] Tue, 24 April 2012 13:26 Go to previous message
Eclipse UserFriend
Thanks James!
Previous Topic:PK Violation on Many-to-One Child Record
Next Topic:Mbean lookup failed in Weblogic 10.3.5 eclipselink 2.1.3.v20110304-r9073
Goto Forum:
  


Current Time: Sat Jul 05 08:49:47 EDT 2025

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

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

Back to the top