-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: @OneToMany vs. one-to-many
PostPosted: Wed Jul 08, 2009 10:18 pm 
Newbie

Joined: Wed Jun 24, 2009 11:34 am
Posts: 13
I am trying to create a Flex/Java/Hibernate project using mySQL, BlazeDS and Tomcat server. I have gotten all of that to work. But as soon as I put in tags for @OneToMany, my project breaks. A lot of the example projects I have seen have created a hbm file with one-to-many mappings. But they are usually much older websites or blog writings, like from 2006 or 2007. So my question is, in the current day, how would I set up a project that needs to map a java file to a OneToMany mapping.
AbstractUser.java:
Code:
public class AbstractUser {
                
   @Id                   
   @GeneratedValue(strategy = GenerationType.AUTO)               
   @Column(name = "userID", nullable = false)                         
   private Long userID;
                  
   @Basic                     
   @Column(name = "first_name", nullable = true, unique = false)                         
   private String firstName;
                    
   @Basic                     
   @Column(name = "last_name", nullable = true, unique = false)                       
   private String lastName;
                        
   @Basic                   
   @Column(name = "type", nullable = true, unique = false)                       
   private String type;
   
   @Basic                   
   @Column(name = "username", nullable = true, unique = false)                       
   private String username;
   
   @Basic                   
   @Column(name = "password", nullable = true, unique = false)                       
   private String password;
                  
   @CollectionOfElements(targetElement = com.pegasus.tms.materials.CourseSession.class)
   @OneToMany(mappedBy="abstractUser", targetEntity=com.pegasus.tms.materials.CourseSession.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
   Collection<CourseSession> courses;


CourseSession.java:
Code:
public class CourseSession {
       
   @ManyToOne(fetch=FetchType.EAGER)
   private AbstractUser abstractUser;
                  
   @Basic                     
   @Column(name = "courseID", nullable = true, unique = false)                         
   private Long courseID;
   
   @Basic                     
   @Column(name = "percentComplete", nullable = true, unique = false)                         
   private int percentComplete;
                    
   @Basic                     
   @Column(name = "lastPage", nullable = true, unique = false)                       
   private String lastPage;
                        
   @Basic                   
   @Column(name = "expiration", nullable = true, unique = false)                       
   private String expiration;
   
   public CourseSession() {   
      super();                          
   }


Again the second I add in the @ManyToOne and @OneToMany, I get the following persistence error in Flex:
Quote:
[RPC Fault faultString="javax.persistence.PersistenceException : [PersistenceUnit: tms] Unable to configure EntityManagerFactory" faultCode="Server.Processing" faultDetail="null"]
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()
at mx.rpc::Responder/fault()
at mx.rpc::AsyncRequest/fault()
at NetConnectionMessageResponder/statusHandler()
at mx.messaging::MessageResponder/status()


TIA


Top
 Profile  
 
 Post subject: Re: @OneToMany vs. one-to-many
PostPosted: Thu Jul 09, 2009 3:10 pm 
Newbie

Joined: Wed Jun 24, 2009 11:34 am
Posts: 13
This topic has been fixed. I needed more configurations, so where my service was called I put:
Code:
public AbstractUser login(String username, String password){
      
      SessionFactory sessionFactory;
      sessionFactory = new AnnotationConfiguration()
        .addAnnotatedClass(AbstractUser.class)
        .addAnnotatedClass(CourseSession.class)
        .addAnnotatedClass(Course.class)
        .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
        .buildSessionFactory();
      sessionFactory.openSession();

      EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);                             
      EntityManager em = emf.createEntityManager();                         
      Query q = em.createNamedQuery("users.byUnPass");                         
      q.setParameter("username", username);   
      q.setParameter("password", password);
      AbstractUser user;
      try{
         user = (AbstractUser) q.getSingleResult();
      }catch(javax.persistence.NoResultException e){
         user = new AbstractUser();
      }
      
      return user;
   }

That works. But I also read about creating a hibernate.cfg.xml file. So I wrote the following:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>


   <session-factory>

      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://www.pegasus-fly.net/tms</property>
      <property name="hibernate.connection.username">myUsername</property>
      <property name="hibernate.connection.password">myPassword</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
      <property name="hibernate.show_sql">true</property>
      <property name="current_session_context_class">thread</property>
      <!-- Persistent entity mappings -->

      <mapping class="com.pegasus.tms.AbstractUser.class"/>
      <mapping class="com.pegasus.tms.materials.Course.class"/>
      <mapping class="com.pegasus.tms.materials.CourseSession.class"/>
      
   </session-factory>   

</hibernate-configuration>

And I am getting the following warning:
Quote:
org.hibernate.connection.UserSuppliedConnectionProvider - No connection properties specified - the user must supply JDBC connections

So I am wondering, is my cfg.xml file even being read in, because I do have the connection provided in the xml file. Can anyone shed light on whether I am approaching this correctly or not? Also, less likely, if anyone has Flex experience, how can I get the cfg file to work with Flex?


Top
 Profile  
 
 Post subject: this might help..
PostPosted: Thu Jul 09, 2009 3:35 pm 
Newbie

Joined: Mon Jul 06, 2009 10:38 am
Posts: 17
http://courses.coreservlets.com/Course-Materials/pdf/hibernate/10-hibernate-JPA.pdf
This article shows how to take a hibernate cfg.xml file and USE that to create a persistent unit (persistence.xml). Not sure if this helps.


Top
 Profile  
 
 Post subject: Re: @OneToMany vs. one-to-many
PostPosted: Thu Jul 09, 2009 4:28 pm 
Newbie

Joined: Wed Jun 24, 2009 11:34 am
Posts: 13
Yeah, that is an interesting article. I bookmarked it because it explains a lot of hibernate in a way I have not seen yet.

What I am not sure of from the article, is it seems to draw a parallel between entityManager and sessionFactory. So which one is preferred to use? Once I added in the sessionFactory to my project, it magically allowed my @OneToMany calls which I need, because I have links between three tables in my database which I need to cross reference for the project.

Further, it seems that persistence.xml is used more in coordination with the entityManager whereas the hibernate.cfg.xml is used with session factory, but then the cfg.xml file was then brought into the persistence.xml file, on the pass through slide:
Code:
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>

So why would you want to do this? Should I combine my two files like this?

Thanks for the response lucky, and TIA for all those to respond next.

BTW - I added in the line of code to my persistence.xml file and it failed, giving me persistence errors again.


Top
 Profile  
 
 Post subject: Re: @OneToMany vs. one-to-many
PostPosted: Thu Jul 09, 2009 7:07 pm 
Newbie

Joined: Mon Jul 06, 2009 10:38 am
Posts: 17
have you installed all the proper Hibernate Jar's? I believe you need 3 of them; Hibernate Core, Hibernate Annotations, and Hibernate Entity Manager. You're probably missing one of those. I just got into hibernate, and am using sessionFactory's. From the reading I have done it appears that the the JPA API (using EntityManager) is the way to go, because your code becomes more 'portable'. I'm currently using Tomcat + Java + Hibernate Annotations + AJAX + MySQL for a Web App and haven't yet tried to load an EJB3. I'm still trying to find a good article on how to setup Tomcat + EJB3 + MySQL.


Top
 Profile  
 
 Post subject: Re: @OneToMany vs. one-to-many
PostPosted: Thu Jul 09, 2009 10:35 pm 
Newbie

Joined: Wed Jun 24, 2009 11:34 am
Posts: 13
LuckY07 wrote:
have you installed all the proper Hibernate Jar's? I believe you need 3 of them; Hibernate Core, Hibernate Annotations, and Hibernate Entity Manager.

Yeah, I have the jar files, all of the articles I read about installation always talk about the same jars, sometimes more than those, for example the ejb3.jar, but I have hibernate3, both annotations jars, and entity manager.

I was using just the entity manager when my database mappings did not use the @OneToMany and @ManyToOne annotations. So once I added in the sessionFactory, it cleared up my errors. Now I am trying to trace information out to see if the mappings worked correctly, but I cannot find the data yet, but that part of it is just Flex stuff, I feel more comfortable with. So I guess the way to go is to have a sessionFactory and an entityManager. Thanks for the tips Lucky.


Top
 Profile  
 
 Post subject: how are you using entityManager and sessionFactory..
PostPosted: Fri Jul 10, 2009 9:30 am 
Newbie

Joined: Mon Jul 06, 2009 10:38 am
Posts: 17
Are you using Tomcat? I was wondering how you setup the entityManager part. Do you have sessionFactory and entityManager working together?


Top
 Profile  
 
 Post subject: Re: @OneToMany vs. one-to-many
PostPosted: Mon Jul 13, 2009 5:52 pm 
Newbie

Joined: Wed Jun 24, 2009 11:34 am
Posts: 13
I am using tomcat and got both the EntityManager and the sessionFactory working together. And they seem like they are working well. I got all of the mappings to work and everything turned out well in Flex. The one thing I need to research from here is the speed. I press the submit button on my project to lookup a username and password from the mySQL db, and it crawls for about a minute until it verifies my code. I am not sure what the time lag is due to, but I will look into that later. Thanks for your help Lucky.

Todd


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.