I am getting an error with a oneToMany association

2019-03-28 10:51发布

I'm using Gilead to persist my entities in my GWT project, im using hibernate annotations aswell. my problem is on my onetomany association.this is my User class that holds a reference to a list of FileLocations

@Entity
@Table(name = "yf_user_table")
public class YFUser implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id",nullable = false)
private int userId;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "email")
private String email;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name="USER_ID") private List fileLocations = new ArrayList();

This is my file location class

@Entity
@Table(name = "fileLocationTable")
public class FileLocation implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "locationId", updatable = false, nullable = false)
private int ieId;
@Column (name = "location")
private String location;

@ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="USER_ID", nullable=false) private YFUser uploadedUser;

When i persist this data in a normal desktop application, it works fine , creates the tables and i can add and store data to it. but when i try to persist the data in my gwt application i get errors i will show them lower. this is my ServiceImpl class that extends PersistentRemoteService.

public class TestServiceImpl extends PersistentRemoteService implements TestService {

public static final String SESSION_USER = "UserWithinSession";

public TestServiceImpl(){

    HibernateUtil util = new HibernateUtil();
        util.setSessionFactory(com.example.server.HibernateUtil
                .getSessionFactory());

    PersistentBeanManager pbm = new PersistentBeanManager();
    pbm.setPersistenceUtil(util);
    pbm.setProxyStore(new StatelessProxyStore());

    setBeanManager(pbm);
}

    @Override
public String registerUser(String username, String password, String email) {
    Session session = com.example.server.HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    YFUser newUser = new YFUser();
    newUser.setUsername(username);newUser.setPassword(password);newUser.setEmail(email);

    session.save(newUser);
    session.getTransaction().commit();

    return "Thank you For registering "+ username;

}

this is the error that im am getting. the error goes away when i remove my onetoManyRelationship and builds my session factory when i put it in , it on the line of buildsessionfactory in hibernate Util that it throws this exception. my hibernate util class is ok also. this is the error java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z

Mar 24, 2010 10:03:22 PM org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.5.0-Beta-4
Mar 24, 2010 10:03:22 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.5.0-Beta-4
Mar 24, 2010 10:03:22 PM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Mar 24, 2010 10:03:22 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : javassist
Mar 24, 2010 10:03:22 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Mar 24, 2010 10:03:22 PM org.hibernate.annotations.common.Version <clinit>
INFO: Hibernate Commons Annotations 3.2.0-SNAPSHOT
Mar 24, 2010 10:03:22 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Mar 24, 2010 10:03:22 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Mar 24, 2010 10:03:22 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Mar 24, 2010 10:03:22 PM org.hibernate.cfg.search.HibernateSearchEventListenerRegister             enableHibernateSearch
INFO: Unable to find org.hibernate.search.event.FullTextIndexEventListener on the     classpath. Hibernate Search is not enabled.
Mar 24, 2010 10:03:22 PM org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: com.example.client.Entity1
Mar 24, 2010 10:03:22 PM org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity com.example.client.Entity1 on table entityTable1
Mar 24, 2010 10:03:22 PM org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: com.example.client.YFUser
Mar 24, 2010 10:03:22 PM org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity com.example.client.YFUser on table yf_user_table
Initial SessionFactory creation failed.java.lang.NoSuchMethodError:     javax.persistence.OneToMany.orphanRemoval()Z
[WARN] Nested in javax.servlet.ServletException: init:
java.lang.ExceptionInInitializerError
at com.example.server.HibernateUtil.<clinit>(HibernateUtil.java:38)
at com.example.server.TestServiceImpl.<init>(TestServiceImpl.java:29)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39 )
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:463)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:396)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
Caused by: java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z
at  org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1642)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:772)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:629)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:350)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1373)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:973)
at com.example.server.HibernateUtil.<clinit>(HibernateUtil.java:33)
... 26 more
[WARN] Nested in java.lang.ExceptionInInitializerError:
java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z
at    org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1642)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:772)
at  org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:629)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:350)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1373)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:973)
at com.example.server.HibernateUtil.<clinit>(HibernateUtil.java:33)
at com.example.server.TestServiceImpl.<init>(TestServiceImpl.java:29)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at  sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:463)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:396)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)

4条回答
时光不老,我们不散
2楼-- · 2019-03-28 11:30

Also if you're compiling with a "complete" Java EE API library (e.g. one that contains both JPA and other APIs such as Servlet etc) then make sure the complete library is further down the classpath than the specific JPA 2.0 library

查看更多
疯言疯语
3楼-- · 2019-03-28 11:43

use javax.persistence api 2.0, but not 1.0 one

查看更多
地球回转人心会变
4楼-- · 2019-03-28 11:47

Just adding a comment to possibly help others facing this. It could also be that your application server provides a version of JPA that "overshadows" the hibernate version. For me I had to do something like this:

mkdir /usr/local/resin/libremoved
mv /usr/local/resin/lib/jpa* /usr/local/resin/libremoved/

查看更多
放荡不羁爱自由
5楼-- · 2019-03-28 11:48

Deleting ejb3-persistance.jar and removing it from build path solved my problem, it may work for you too.

Also don't forget to add hibernate-jpa-2.0-api-1.0.0.Final.jar to your build path.

查看更多
登录 后发表回答