I have an auto-generated sequence in my model class like this:
@Entity
@Table(name="track")
@NamedQuery(name="track.findAll", query="SELECT t FROM Track t")
public class Track {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private long id;
...
I want to display the next value in an "insert.jsp" -somewhat like this:
<table>
<form:form action="save.do" method="post" modelAttribute="track">
<tr>
<td>Track Id:</td>
<td><form:input path="id" value="${track.getNext()}" readonly="true"/></td>
</tr>
{track.getNext()} doesn't work as of now. What is the best way to do this? I tried using this piece of code in my model class. My context lookup path is throwing an error. Is this the right place to write this code? If not, where should I put it? What should be the context lookup path? In my code, it is the path to Persistence.xml.
@PersistenceContext
public static long getNext() throws Exception {
Context initCtx = new InitialContext();
javax.persistence.EntityManager em = (javax.persistence.EntityManager)
initCtx.lookup("src.main.resources");
Query q = (Query) em.createNativeQuery("SELECT TRACK_ID_SEQ.nextval from DUAL");
BigDecimal result=(BigDecimal)((javax.persistence.Query) q).getSingleResult();
return result.longValue();
}