- This topic has 8 replies, 4 voices, and was last updated 20 years, 2 months ago by cbruin.
-
AuthorPosts
-
Tom MaloneMemberI have linux running jdk 1.5.0 ( though before you ask i have the same problem under j2ee1.4 and j2sdk1.4.2 ) mysql 3.23.58, (though again have same problem under mysql 4.0.20).
When you create a mapping from a table with an int as a primary key, it creates a string in the mapping, which i think, means that when i get the following error when i run a simple app.
log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" java.lang.RuntimeException: net.sf.hibernate.id.IdentifierGenerationException: this id generator generates long, integer, short at co.uk.fastest.foi.hibernate.Database.addThirdPartys(Database.java:297) at Application.<init>(Application.java:26) at Application.main(Application.java:32) Caused by: net.sf.hibernate.id.IdentifierGenerationException: this id generator generates long, integer, short at net.sf.hibernate.id.IdentifierGeneratorFactory.get(IdentifierGeneratorFactory.java:38) at net.sf.hibernate.persister.AbstractEntityPersister.getGeneratedIdentity(AbstractEntityPersister.java:1231) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:530) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:432) at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29) at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:932) at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857) at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:775) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738) at co.uk.fastest.foi.hibernate.Database.addThirdPartys(Database.java:291)
I assume the fact that the string is to blame. The other really odd thing is that it does add it to the database. My sql is
drop table if exists Applicants; create table Applicants ( applicant_id int unsigned not null primary key auto_increment, applicant_title varchar(10), applicant_first_name varchar(25), applicant_initials varchar(5), applicant_surname varchar(25) not null, applicant_house varchar(50), applicant_house_number bigint unsigned, applicant_address_1 varchar(50), applicant_address_2 varchar(50), applicant_town varchar(50) , applicant_county varchar(50), applicant_postcode varchar(10), applicant_country varchar(50), applicant_organisation bigint unsigned, applicant_job_title varchar(100), applicant_telephone_home bigint unsigned, applicant_telephone_work bigint unsigned, applicant_fax bigint unsigned, applicant_mobile bigint unsigned, applicant_email varchar(125) , applicant_accessability text not null, foreign key (applicant_organisation) references Organisation(organisation_id) );
I generated this using myeclipseide 3.8.1, hibernate tools with a HibernateSessionFactory, wrote this addThirdPartys method
public void addThirdPartys ( ThirdPartys third ) { // start of the addThirdPartys method // Create instance of session Session session = null; try { // start of the try statement session = HibernateSessionFactory.currentSession(); session.save(third); } // end of try statement catch ( HibernateException e ) { // start of the catch exception throw new RuntimeException ( e ); } // end of the catch exception finally { // start of the finally statement if ( session != null ) { // start of if statement try { // start of the try statement session.close ( ); } // end of the try statement catch ( HibernateException e ) { // start of the catch statement throw new RuntimeException ( e ); } // end of the catch statement } // end of the if statement } // end of the finally statement } // end of the addThirdPartys method
And the app is
/** * @author tom * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ import co.uk.fastest.foi.hibernate.*; //import java.util.List; public class Application { public Application () { ThirdPartys third = new ThirdPartys(); third.setThirdOrg("Thames Valley"); Database.getInstance().addThirdPartys(third); } public static void main ( String args[] ) { new Application ( ); } }
My question is what am i doing wrong.
Riyad KallaMemberI have asked our Hib dev to look at this.
Tom MaloneMemberthanks
Tom MaloneMemberThe problem lies in the fact all integers are set as strings, so in the mapping they need to be changed to integers. The original mapped file looked like this
<hibernate-mapping package="co.uk.fastest.foi.hibernate"> <class name="ThirdPartys" table="Third_Partys"> <id name="thirdId" column="third_id" type="java.lang.String"> <generator class="native"/> </id> <property name="thirdOrg" column="third_org" type="java.lang.String" not-null="true" /> <property name="thirdConsentDate" column="third_consent_date" type="java.util.Date" /> </class>
if you change it to this it works fine.
<hibernate-mapping package="co.uk.fastest.foi.hibernate"> <class name="ThirdPartys" table="Third_Partys"> <id name="thirdId" column="third_id" type="integer"> <generator class="native"/> </id> <property name="thirdOrg" column="third_org" type="integer" not-null="true" /> <property name="thirdConsentDate" column="third_consent_date" type="java.util.Date" /> </class>
You also have to change all the strings to ints like below
/* * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized * by MyEclipse Hibernate tool integration. * * Created Fri Sep 03 15:12:53 BST 2004 by MyEclipse Hibernate Tool. */ package co.uk.fastest.foi.hibernate; import java.io.Serializable; /** * A class that represents a row in the Third_Partys table. * You can customize the behavior of this class by editing the class, {@link ThirdPartys()}. * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized * by MyEclipse Hibernate tool integration. */ public abstract class AbstractThirdPartys implements Serializable { /** The cached hash code value for this instance. Settting to 0 triggers re-calculation. */ private int hashValue = 0; /** The composite primary key value. */ private int thirdId; /** The value of the simple thirdOrg property. */ private int thirdOrg; /** The value of the simple thirdConsentDate property. */ private java.util.Date thirdConsentDate; /** * Simple constructor of AbstractThirdPartys instances. */ public AbstractThirdPartys() { } /** * Constructor of AbstractThirdPartys instances given a simple primary key. * @param thirdId */ public AbstractThirdPartys(int thirdId) { this.setThirdId(thirdId); } /** * Return the simple primary key value that identifies this object. * @return java.lang.String */ public int getThirdId() { return thirdId; } /** * Set the simple primary key value that identifies this object. * @param thirdId */ public void setThirdId(int thirdId) { this.hashValue = 0; this.thirdId = thirdId; } /** * Return the value of the third_org column. * @return java.lang.String */ public int getThirdOrg() { return this.thirdOrg; } /** * Set the value of the third_org column. * @param thirdOrg */ public void setThirdOrg(int thirdOrg) { this.thirdOrg = thirdOrg; } /** * Return the value of the third_consent_date column. * @return java.util.Date */ public java.util.Date getThirdConsentDate() { return this.thirdConsentDate; } /** * Set the value of the third_consent_date column. * @param thirdConsentDate */ public void setThirdConsentDate(java.util.Date thirdConsentDate) { this.thirdConsentDate = thirdConsentDate; } /** * Implementation of the equals comparison on the basis of equality of the primary key values. * @param rhs * @return boolean */ /*public boolean equals(Object rhs) { if (rhs == null) return false; if (! (rhs instanceof ThirdPartys)) return false; ThirdPartys that = (ThirdPartys) rhs; if (this.getThirdId() != null && that.getThirdId() != null) { if (! this.getThirdId().equals(that.getThirdId())) { return false; } } return true; }*/ /** * Implementation of the hashCode method conforming to the Bloch pattern with * the exception of array properties (these are very unlikely primary key types). * @return int */ /* public int hashCode() { if (this.hashValue == 0) { int result = 17; int thirdIdValue = this.getThirdId() + this.getThirdId().hashCode(); result = result * 37 + thirdIdValue; this.hashValue = result; } return this.hashValue; } */ }
I haven’t quite sorted out the hashcodes and equals methods yes, i think i’ll cross thoose bridges if i come to them. This code works fine now.
You may want to fix this for the next release.
Keep up the good work this is a fantastic extention to eclipse.
Tom
support-jeffMemberWhen I get a chance I will check this out under mysql 3.23. This looks really wrong.
cbruinMemberi think this also happened in hiberclipse, another hibernate plugin, so it may not be solely myeclipse
i tried it the other day and it gave me a string as the primary key in my mappings even though it was an int in mysql
support-jeffMembervery interesting. Could be a mysql/connectorJ thing? Still need to do some testing…
support-jeffMemberOk. DId some testing in mysql 4.0.20 using the latest greatest Connector/J JDBC driver (3.0.15) and I cannot reproduce your problem. Gets the ints and the relationship between Applicants and Organisations just right.
The 3.0.15 version of the driver just came out in the last day or so and I note on the release notes that there were some DB metadata issues resolved. See here: http://dev.mysql.com/doc/connector/j/en/#id2430795. So this may have been resolved for you by the MySQL Connector/J developers.
cbruinMemberAh, thank you! I also had to change my hibernate settings because it was using the org.mm.mysql driver instead of the com.mysql.jdbc.Driver one.
For people with the same issues, go to the base directory of your workspace. Open “.myhibernatedata” in a text editor and change your jar file to the 3.0.15 mysql connector jar location.
also update your mysql driver name.
it now generates it as java.lang.Integer rather than java.lang.String 🙂
Thanks for the tip! i never would have updated my driver if you hadn’t pointed out that a .01 version difference would have helped. (or maybe it was even an org.mm.mysql problem eh?)
-
AuthorPosts