Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Const Mapping(Jpa+Const Mapping (not enum))
Const Mapping [message #850720] Fri, 20 April 2012 09:04 Go to next message
Anatoliy Devyatkin is currently offline Anatoliy DevyatkinFriend
Messages: 2
Registered: April 2012
Junior Member
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 04:18]

Report message to a moderator

Re: Const Mapping [message #854101 is a reply to message #850720] Mon, 23 April 2012 17:08 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

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.


James : Wiki : Book : Blog : Twitter
Re: Const Mapping [message #855291 is a reply to message #854101] Tue, 24 April 2012 17:26 Go to previous message
Anatoliy Devyatkin is currently offline Anatoliy DevyatkinFriend
Messages: 2
Registered: April 2012
Junior Member
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: Fri Apr 19 01:23:41 GMT 2024

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

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

Back to the top