How to update schema by adding Annotated classes to an existing Persistence unit in JPA with hibernate?
Environment: JPA 2.0, Hibernate 3.6, in a war file (with jsf 2.0, on jboss 6, etc).
I want update schema by adding Annotated classes to an existing Persistence unit in JPA with hibernate. this is what I have tried to do:public void updateConfiguration() {
// load classes
Ejb3Configuration ejb3Configuration = new Ejb3Configuration();
ejb3Configuration = ejb3Configuration.configure("existingPU", null);
loadModel(ejb3Configuration);
updateSchema(ejb3Configuration.getHibernateConfiguration());
}
private void updateSchema(Configuration cfg) {
SchemaUpdate schemaUpdate = new SchemaUpdate(cfg);
schemaUpdate.execute(true, true);
}
private void loadModel(Ejb3Configuration ejb3Configuration) {
for (Object _object : objectList) {
ejb3Configuration.addAnnotatedClass(_object.getClass());
}
}
The existingPU
is loaded by the normal JPA way using persistence.xml etc. The schema is updated for the entities in WEB-INF/classes
and that part of the code base works perfectly. Now, the new classes in the objectList
extend a @MappedSuperclass
from WEB-INF/classes
which has
@MappedSuperclass
public abstract class OwnerHolder implements Serializable {
private User owner;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "fk_owner_id")
public User getOwner() {
return owner;
}
public void setOwner(User owner) {
this.owner= owner;
}
}
This class is also extended by classes in WEB-INF/classes and that code works fine. The error I get is as follows:
13:36:45,494 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] could not complete schema update: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.myproject.external.model.PurchaseOrder.owner references an unknown entity: com.myproject.model.User
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:107) [:3.6.0.Final]
at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1550) [:3.6.0.Final]
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1473) [:3.6.0.Final]
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1389) [:开发者_开发百科3.6.0.Final]
at org.hibernate.cfg.Configuration.generateSchemaUpdateScript(Configuration.java:1160) [:3.6.0.Final]
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:187) [:3.6.0.Final]
at com.myproject.MyConfiguration.updateSchema(MyConfiguration.java:43) [:]
The objectList
is list of classes from a jar file placed in WEB-INF/lib.
Can you please help me?
Check if the User class hast the @Entity annotation in its declaration.
精彩评论