Reputation: 4864
Caused by: org.hibernate.AnnotationException: Unknown Id.generator: com.agitech.erp.model.act.JournalPeriodIdGenerator
Why is hibernate not able to find my IdentifierGenerator, is there an annotation to add? or should I add it during the configuration and how?
public class JournalPeriodIdGenerator implements IdentifierGenerator {
private static Long id = new Long(1); // just for test it should return a composite id
public JournalPeriodIdGenerator(){
}
@Override
public Serializable generate(SessionImplementor session, Object object)throws HibernateException {
//session.list( "select ", queryParameters)
return id++;
}
}
@Entity
public class ActEntry extends Record implements Comparable<ActEntry>,Serializable { @GeneratedValue(generator="com.agitech.erp.model.act.JournalPeriodIdGenerator")
@EmbeddedId
private JournalPeriodId codeId;
public JournalPeriodId getCodeId() {
return codeId;
}
public void setCodeId(JournalPeriodId codeId) {
this.codeId = codeId;
}
}
following JB Nizet help, I also try (which fails too with same exception )
@GenericGenerator(name="JournalPeriodIdGenerator", strategy="increment")
public class JournalPeriodIdGenerator implements IdentifierGenerator {
....
}
@Entity
public class ActEntry extends Record implements Comparable<ActEntry>,Serializable {
private static final long serialVersionUID = 1L;
@GeneratedValue(generator="JournalPeriodIdGenerator")
@EmbeddedId
private JournalPeriodId codeId;
}
Following the second suggestion I try :
@Entity
public class ActEntry extends Record implements Comparable<ActEntry>,Serializable {
@GenericGenerator(name="JournalPeriodIdGenerator", strategy="jrn_increment")
public class JournalPeriodIdGenerator implements IdentifierGenerator {
.....
}
@GeneratedValue(generator="JournalPeriodIdGenerator")
@EmbeddedId
private JournalPeriodId codeId;
...
}
another test
@Entity
public class ActEntry extends Record implements Comparable<ActEntry>,Serializable {
@GeneratedValue(generator="JournalPeriodIdGenerator")//with com.agitech.erp.model.act.JournalPeriodIdGenerator not working too
@EmbeddedId
private JournalPeriodId codeId;
....
}
@Entity
@GenericGenerator(name="JournalPeriodIdGenerator", strategy="com.agitech.erp.model.act.JournalPeriodIdGenerator")
public class JournalPeriodIdGenerator implements IdentifierGenerator {
....
}
Upvotes: 1
Views: 6617
Reputation: 4864
with the help of JB Nizet and folowing this blog http://blog.eyallupu.com/2011/01/hibernatejpa-identity-generators.html
@Entity
public class ActEntry extends Record implements Comparable<ActEntry>,Serializable {
// here is the definition that JB Nizet is talking about !
@GenericGenerator(name="JournalPeriodIdGenerator",
strategy="com.agitech.erp.model.act.JournalPeriodIdGenerator")
@GeneratedValue(generator="JournalPeriodIdGenerator")
@EmbeddedId
private JournalPeriodId codeId;
.....
}
@GenericGenerator(name="JournalPeriodIdGenerator", strategy="increment")
public class JournalPeriodIdGenerator implements IdentifierGenerator {
....
}
Upvotes: 1
Reputation: 691635
The javadoc of GeneratedValue says:
generator : (Optional) The name of the primary key generator to use as specified in the SequenceGenerator or TableGenerator annotation.
To define your own generator, you would use GenericGenerator, and then reference the name of the GenericGenerator in GeneratedValue
.
Upvotes: 0