0

I am using JPA2.0 and Hibernate 4

For generating primary key in table, I am using table trigger. Trigger works fine if I am using Long as data type for Primary Key. However if I am using String as Primary Key data type, then I am getting the following errors.

org.springframework.orm.hibernate3.HibernateSystemException: Unknown integral 
data type for ids : java.lang.String; nested exception is
org.hibernate.id.IdentifierGenerationException: Unknown integral
 data type for ids : java.lang.String at 
org.springframework.orm.hibernate3.SessionFactoryUtils.
convertHibernateAccessException(SessionFactoryUtils.java:690)

So is it not allowed to use String for Primary Key for generating values using trigger?

My code snippet for generating values using trigger

private String deptNo;
@Id
@GenericGenerator(name = "trig", strategy = "increment")
@GeneratedValue(generator = "trig")
@Column(name = "DEPT_NO")

  public String getDeptNo() {
    return deptNo;
}

public void setDeptNo(String deptNo) {
    this.deptNo = deptNo;
}

1 Answer 1

2

you are not allowed to use generated values for String in hibernate, however there is no limitation for keys. if you have to use deptNo as a string you can create an adaptor class for your Domain object which contains deptNo as Long.

Sign up to request clarification or add additional context in comments.

4 Comments

dursun What exactly you mean by using String as an adaptor class? You mean creating custom class for generating keys?
no, I mean you create another object for your current class, for example if your current class is Employee, you can create AdaptorEmployee which converts Employees deptNo attribute which is Long into a String.
So when I am writing to database I should call AdaptorEmployee class?
you should use adapter whenever you need deptNo as string and you should use the actual class whenever you need deptNo as long. So when you are intracting with db you need deptNo as long.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.