- This topic has 1 reply, 2 voices, and was last updated 19 years, 11 months ago by support-jeff.
-
AuthorPosts
-
tarantulaParticipantHi,
I’m running Eclipse 3.0.1 with MyEclipse 3.8.2 on a Fedora Core Linux system and I am running into a problem with Hibernate while using a MySQL database using the MyISAM storage engine.
Here’s the situation. First of all, I know that your tool is supposed to be used with databases that support foreign keys. If I had the choice I would be using MySQL’s InnoDB storage engine, but my client’s host forces us to use MyISAM.
Running a copy of my database locally using InnoDB, I generated my model code and proceeded to build my Java application. It worked fine until I switched to the MyISAM copy of the database and now I get a HibernateException when I execute my code (edited for brevity):
net.sf.hibernate.UnresolvableObjectException: No row with the given identifier exists: 0, of class: myproject.MyClass at net.sf.hibernate.UnresolvableObjectException.throwIfNull(UnresolvableObjectException.java:38) at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1943) at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:68) at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:232) at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2194) at net.sf.hibernate.loader.Loader.doQuery(Loader.java:240) at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133) at net.sf.hibernate.loader.Loader.doList(Loader.java:955) at net.sf.hibernate.loader.Loader.list(Loader.java:946) at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:834) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1536) at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39) at org.cfuw.util.HibernateUtil.fetchList(HibernateUtil.java:30) ...
Any thoughts on this? I would really appreciate some advice on how to work around the lack of foreign key support in MyISAM tables while using the MyEclipse Hibernate tool.
tarantula
support-jeffMembertarantula –
As far as the Hibernate tooling goes, if you are developing against an InnoDB database, you will get many-to-one relationships added to your mappings automatically whenever a FK is found for a table. To run these against your non-InnoDB database, you are going to need to either manually replace the many-to-one element with a property referencing the FK id in your mapping file, or it may be easier to just develop against a non-InnoDB version of MySQL.
Either way you go, you will have to manually load relationships. So for example, if you had an ORDER table with a FK to a CUSTOMER table such that ORDER had a column, customer_id, then the mapping file should refer to a <property> mapped to the customer_id column. Likewise, your Java class needs to have a property with the same name as the property in the mapping. This property automatically gets populated from the db by Hibernate. Now, say you want to have an Order.getCustomer() method that returns the related Customer object. You have many options on how to do this since you have to code it. You could:
* lazy-load the Customer object when getCustomer() is called; or
* load it up front when the Order is instantiated (be careful of nested connection usage); or
* lazy-init the Customer after Hibernate has loaded the Order for you.The point is that somewhere you are going to have to code the loading of a Customer rather than it being automatically loaded by Hibernate:
Order order = session.load(Order.class, orderId); Customer customer = session.load(Customer.class, order.getCustomerId()); order.setCustomer(customer);
Make sense?
jeff
-
AuthorPosts