I'm working on a Java/Spring/Hibernate based web app, developed in Eclipse.
This web app is actually two different projects that duplicate a couple Hibernate entity classes, as well as their respective Hibernate repository classes. Obviously, it would be nice to take the duplicated code, and move it into an external JAR file or something.
From the Hibernate documentation, I know that I'm supposed to put the entity class in a new JAVA project that contains a persistence.xml
file. I also know that I can then export this project to a JAR, and include it in the classpath of my main projects. Further, I know that I'm supposed to reference this JAR from my hibernate.cfg.xml
in my main project.
For some reason, it doesn't seem to be quite so simple with the project I'm working on right now.
Here are the questions:
in my external project that holds the shared code, can I include the Hibernate Repository class it it too? How should this be noted in the
persistence.xml
, if it indeed needs to be at all?In my external JAR, how minimal can the
persistence.xml
be? For instance, the main project already configures the hibernate dialect, so can I leave that out of this external JAR'spersistence.xml
?From the Hibernate docs, I know that I can reference my external JAR file using the
<jar-file>
tag in myhibernate.cfg.xml
. So what if my project doesn't have ahibernate.cfg.xml
? We have a configuration xml for Spring that gives anannotatedClasses
property to theorg.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
. I'm not having much luck finding examples of referencing external JAR files with theAnnotationSessionFactoryBean
.
I've tried searching around on this site, but haven't found anything similar enough to help me out yet. Apologies if this is a duplicate post, or a stupidly obvious question or something.
Thanks in advance :)
EDIT:
OK, so it looks like what I ultimately need to know is, supposing I've included "MY_TEST.jar"
in my classpath, and further supposing "MY_TEST.jar"
has one annotated Hibernate class: src.shared.myEntity.java
How would I go about referencing this entity class with the below Spring configuration excerpt? (keep getting IllegalArgumentException: Cannot find class
pretty much no matter what I try)
<bean id="my.postgres.sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource"
ref="my.postgres.dataSource" />
<property name="annotatedClasses">
<list>
<value>src.app.Entit开发者_开发问答yA</value>
<value>src.app.EntityB</value>
</list>
</property>
.........
</bean>
Hibernate doesn't have the concept of a repository. Spring does.
Hibernate doesn't care where the persistent entities classes are. They must be in the classpath, that's all. Whether they're in one, two, or three jars doesn't matter if all these jars are in the classpath.
Organize your projects as you would like and just make sure you don't have two different hibernate configuration files in the classpath. I would use a project to hold all the common entities and maybe their associated repositories and let the main project define the hibernate configuration file.
If the shared class fully qualified name is src.shared.myEntity, then the spring config should have the following lines:
<property name="annotatedClasses">
<list>
...
<value>src.shared.myEntity</value>
</list>
</property>
Your wording is confusing, so I'll check what I considered obvious, but which is perhaps not. The fully qualified name of a class is composed of its full package name, followed by the name of the class, without .class or .java extension. The jar must contain the .class file, not the .java file. The hierarchy of the directories in the jar must exactly map the hierarchy of the packages. So if your entity looks like this:
package src.shared;
@Entity
public class myEntity {
// ...
}
The jar should have,
at its root,
a src
directory,
containing a shared
directory,
containing a myEntity.class
file.
PS: having src
as a root package name is really strange.
Why don't you respect the convention: com.yourcompany.yourproject
...?
PPS: a class name should always start with a capital letter:
MyEntity
and not myEntity
.
精彩评论