facebook

Having trouble setting up Hibernate with MyEclipse

  1. MyEclipse IDE
  2.  > 
  3. General Development
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #284294 Reply

    Mork
    Participant

    This message has not been recovered.

    #284314 Reply

    TonyHH
    Member

    The dialect you are using is a MySQL dialect. Are you using MySQL as the database? You might want to check out some of the tutorials regarding Hibernate – it’s actually quite straight forward.

    #284319 Reply

    Mork
    Participant

    I was using the MyEclipse Hibernate Tutorial when I ran into this problem.

    And, yes, the DB is MySQL.

    I guess I need to start over …

    #284321 Reply

    Mork
    Participant

    MyEclipse / Hibernate can’t find the configuration file — although it it’s in the default directory where MyEclipse put it. I double checked the project properties to make sure the project “knew” about the correct location of the hibernate config file. That was fine.

    No dice. MyEclipse/Hibernate just can’t find it. I also tried to adjust the path in the SessionFactory file.

    Ok, trying to start over…. I removed all the hibernate libs from the project and any of the generated files, CLEANED the project, REFRESHED the project, but still MyEclipse won’t let me add Hibernate Capabilities again. It still thinks this is a Hibernate project.

    How do I convince MyEclipse that Hibernate isn’t in the project anymore?

    Under project properties, it says “Active Configuration File Does Not Exist”.

    Of course it doesn’t exist, I just deleted all the Hibernate stuff.

    You should also have, unless I missed it, a “Remove Hibernate Capabilities” so your customers don’t spend all this time just trying to remove something they “Added”

    What I think also is sorely needed is that MyEclipse should have some basic validation logic to check to see if the installed Hibernate configuration will work. I’m not talking about checking any application logic, but whether the SessionFactory class can be found, the hibernate config xml file can be found, etc..

    I’ve now spent over 10 hours just trying to get a basic example working with MyEclipse. Plus, I’ve now had to start over and now can’t re-add Hibernate capabilities. So, I’m totally stuck.

    It seems that most of the problems are with MyEclipse and the fact it really doesn’t do that much to help you (other than to create some basic classes): no configuration validation, for example.

    M

    #284338 Reply

    Riyad Kalla
    Member

    Mork,

    I just tried a few of the Hibernate projects we have in our EOD repository *and* created a brand new project from scratch and they all worked fine out of the box. My guess is that in your frustration you changed some fundamental path or reference that has been the root cause of all the broken behavior since then and it’s slowly just been making things worse and worse and worse.

    So instead of banging our heads against the wall, let’s start over… not with the same project, but with a brand new one. Can you describe to me the name of your MySQL schema that you are reverse engineering and approximately how many tables are in it? I’ll try and guide you through those steps to get you a working project and also provide some shortcuts on how to make using the generated hibernate classes even easier than I think you realize it will be (for example, there is no reason to construct session factories, just use the DAOs).

    Just for reference I’m attaching an example Hibernate project I created for you. It uses Hibernate and the MyEclipse Derby Server, specifically the “MYBLOG” schema in it to display the posts in the DB. If it’s empty it won’t show anything. This is just for reference.

    Attachments:
    You must be logged in to view attached files.
    #284346 Reply

    Mork
    Participant

    Hello R,

    As always, your support is wonderful! 🙂

    How do I tell MyEclipse for this project that it’s no longer a Hibernate project so I can re-add the libs and start over?

    I need to walk through the tutorial again (obviously <s>) and see where I made my mistake.

    Thanks for your reply.

    M

    #284348 Reply

    Riyad Kalla
    Member

    How do I tell MyEclipse for this project that it’s no longer a Hibernate project so I can re-add the libs and start over?

    At this point I’d encourage you to start over with a new project and not try and salvage that one, but that being said, MyEclipse looks for the natures on the project (defined inside the .project file) to determine what kind of project it is.

    If you remove the line:

    
    <nature>com.genuitec.eclipse.hibernate.hibernatenature</nature>
    

    from the bottom under natures, and then you probably want to remove the hibernate builder, it looks like this:

    
            <buildCommand>
                <name>com.genuitec.eclipse.hibernate.HibernateBuilder</name>
                <arguments>
                </arguments>
            </buildCommand>
    

    then you probably want to close and reopen the project and then you should be able to re-add Hibernate caps to it.

    NOTE: If you keep having problems with that one project, as a first step (Before posting back here) please try and start over with a new project and see if suddenly everything is working as intended.

    #284354 Reply

    Mork
    Participant

    This seems like it could be automated as an “Uninstall Capabilities” feature.

    Yes, this worked!!!!! Thank you. Good thing to know how to do. Again, nice if it could be automated somehow, but I’m sure you all have other higher priorities.

    Unfortunately, this project is the one that needs the Hibernate integration, and I’ve already worked through the tutorial successfully a while back. I’ll let you know what happens. For some reason, the installation didn’t seem to know anything about where to find the hibernate.cfg.xml file. I actually traced the Hibernate source code, but it wasn’t that helpful…

    Thanks again.

    #284373 Reply

    Mork
    Participant

    Hello R,

    Well, it’s working now fine. I’m not sure what changed but when I re-did the Hibernate integration to the project, I can now use the DAO object.

    Thanks very much for your help and patience!

    🙂

    #284500 Reply

    Riyad Kalla
    Member

    No problem, I’m just glad it’s working.

    #284642 Reply

    Mork
    Participant

    I meant to also ask you if the setup you created to test the one-to-many relationship actually populated the “many” side?

    I created a foreign key constraint and got the expected elements in the config files, but the many side isn’t populated.

    The user table code is working fine, but the many table (uservisithistory) doesn’t get populated.

    I think it may be in the mapping files.

    I have the PK generation set to automatic in MySQL with no nulls, etc.

    Here are the updated mapping files (and the code that doesn’t work):

    Users Table

    code:

    <hibernate-mapping>
    <class name=”TestProject.Users” table=”users” catalog=”users”>
    <id name=”usrPk” type=”java.lang.Integer”>
    <column name=”usr_pk” />
    <generator class=”increment” />
    </id>
    <property name=”usrIsPaidUser” type=”java.lang.Byte”>
    <column name=”usr_isPaidUser” not-null=”true” />
    </property>
    <property name=”usrAccountExpires” type=”java.lang.Byte”>
    <column name=”usr_accountExpires” not-null=”true” />
    </property>
    <property name=”usrAccountExpireDate” type=”java.util.Date”>
    <column name=”usr_accountExpireDate” length=”19″ />
    </property>
    <property name=”usrFname” type=”java.lang.String”>
    <column name=”usr_fname” length=”20″ not-null=”true” />
    </property>
    <property name=”usrMi” type=”java.lang.String”>
    <column name=”usr_mi” length=”1″ />
    </property>
    <property name=”usrLname” type=”java.lang.String”>
    <column name=”usr_lname” length=”25″ not-null=”true” />
    </property>
    <property name=”usrEmail” type=”java.lang.String”>
    <column name=”usr_email” length=”45″ />
    </property>
    <property name=”usrPhone” type=”java.lang.String”>
    <column name=”usr_phone” length=”25″ />
    </property>
    <property name=”usrLastlogindate” type=”java.util.Date”>
    <column name=”usr_lastlogindate” length=”19″ />
    </property>
    <property name=”usrAccountactive” type=”java.lang.Byte”>
    <column name=”usr_accountactive” not-null=”true” />
    </property>
    <property name=”usrLogin” type=”java.lang.String”>
    <column name=”usr_login” length=”45″ not-null=”true” />
    </property>
    <property name=”usrPassword” type=”java.lang.String”>
    <column name=”usr_password” length=”20″ not-null=”true” />
    </property>
    <set name=”uservisithistories” inverse=”true”>
    <key>
    <column name=”usr_pk” not-null=”true”>
    <comment>foreign key back to users table.</comment>
    </column>
    </key>
    <one-to-many class=”TestProject.Uservisithistory” />
    </set>
    </class>
    </hibernate-mapping>

    Here is the mapping for the user visit history (many side) table:

    code:

    <hibernate-mapping>
    <class name=”TestProject.Uservisithistory” table=”uservisithistory” catalog=”users”>
    <id name=”uvhPk” type=”java.lang.Integer”>
    <column name=”uvh_pk” />
    <generator class=”increment” />
    </id>
    <many-to-one name=”users” class=”TestProject.Users” fetch=”select”>
    <column name=”usr_pk” not-null=”true”>
    <comment>foreign key back to users table.</comment>
    </column>
    </many-to-one>
    <property name=”uvhDateTimeOfVisit” type=”java.util.Date”>
    <column name=”uvh_dateTimeOfVisit” length=”19″ />
    </property>
    <property name=”uvhPageFrom” type=”java.lang.String”>
    <column name=”uvh_pageFrom” length=”45″ not-null=”true” />
    </property>
    </class>
    </hibernate-mapping>

    The code to create the many side I do inside a test “main” method like this:
    (see: “// add new visit history record” below)

    code:

    // transaction instance created earlier…

    Users newUser = new Users();
    newUser.setUsrAccountactive((byte) 1);

    newUser.setUsrAccountExpires((byte)0);

    // // set expiration date from one year from now
    Calendar now = Calendar.getInstance();
    now.add(Calendar.YEAR, 1);
    Date dateToStore = now.getTime();
    newUser.setUsrAccountExpireDate(dateToStore);

    // set user first name and last name
    newUser.setUsrFname(“admin”);
    newUser.setUsrLname(“admin”);

    // user doesn’t pay.
    newUser.setUsrIsPaidUser((byte) 0);

    // set user login/password
    newUser.setUsrLogin(“admin@test.net”);
    newUser.setUsrPassword(“123456”);

    // add new visit history record
    // ****** CODE BELOW DOES NOT WORK -> No Population in many table

    Uservisithistory uvh = new Uservisithistory();
    uvh.setUsers(newUser);
    uvh.setUvhDateTimeOfVisit(dateToStore);
    uvh.setUvhPageFrom(“home page”);
    HashSet<Uservisithistory> set = new HashSet <Uservisithistory>();
    set.add(uvh);

    newUser.setUservisithistories(set);

    // save user

    dao.save(newUser);
    transaction.commit();

    }

    The strange thing to me is that the mapping file I generated has a “Users” element in the many side.

    I think the reason it might not be populating the many side is that it needs the foreign key to insert into the user visit history table. However, since the PK in the users table gets generated by MySQL automatically, I’m not sure how this works or why the code isn’t populating the many side.

    Does the code, having a Set in the users table, and a Users object in the uservisithistory table, look correct?

    Any advice/suggestions would be greatly appreciated.

    Since there isn’t an example for MyEclipse on this common usage, I’m trying to verify what I’ve done so far makes sense.

    Thanks.

    M

Viewing 11 posts - 1 through 11 (of 11 total)
Reply To: Having trouble setting up Hibernate with MyEclipse

You must be logged in to post in the forum log in