I'm trying to generate an auto ID. My problem is that my ID-generating class uses a String (varchar in db) but the entity being created uses an Integer (numeric in db).
I tried using a converter on the ID-generating class. Although sParamValue is a varchar in our db, I thought I could convert it to Integer, ie:
IDGenerator.class :
@Entity
@Table(name = "idGenerator")
@XmlRootElement
@NamedQueries(
{
//some queries here
})
@Data
public class IDGenerator implements Serializable
{
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 128)
@Column(name = "sParamValue")
@Convert(converter = OptionalIntegerConverter.class)
private Integer sParamValue;
...
}
Class in need of ID:
public class MyClass implements Serializable
{
@Id
@Basic(optional = false)
@NotNull
@GeneratedValue(generator = "TABLE_SEQ", strategy = GenerationType.TABLE)
@TableGenerator(name = "TABLE_SEQ",
table = "idGenerator",
pkColumnName = "sParamName",
valueColumnName = "sParamValue",
pkColumnValue = "NextIdNumber",
allocationSize = 1)
@Column(name = "idNum")
private Integer idNum;
}
Unfortunately, MyClass does not get the converted value of sParamValue but just gets the String directly from the database, and then I get an error:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
at org.eclipse.persistence.sequencing.QuerySequence.updateAndSelectSequence(QuerySequence.java:278)
at org.eclipse.persistence.sequencing.StandardSequence.getGeneratedVector(StandardSequence.java:71)
at org.eclipse.persistence.sequencing.Sequence.getGeneratedVector(Sequence.java:257)
at org.eclipse.persistence.internal.sequencing.SequencingManager$Preallocation_Transaction_NoAccessor_State.getNextValue(SequencingManager.java:549)
Any ideas on how to make a converter work?
I could change the data type of MyClass.idNum to String, use the converter there, and make changes throughout my code, but that's an ugly solution.
I wonder if I could do what is described here and create a custom generator which would query my idGenerator table. It's important that no other access to idGenerator be allowed while I'm getting the latest id. I'm assuming @TableGenerator is transactional, but is the EclipseLink generator?