-->
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.  [ 3 posts ] 
Author Message
 Post subject: Collection Mapping
PostPosted: Mon Nov 07, 2005 5:06 pm 
Newbie

Joined: Thu Oct 06, 2005 9:30 am
Posts: 18
Location: Canada
I have the following two classes

public class A {
int id;
String name;
Set set = new HashSet();

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Set getSet() {
return set;
}

public void setSet(Set set) {
this.set = set;
}

public void addSet(B b){
this.set.add(b);
}

}
************************************
<class name="A" table="A" >
<id name="id" column="a_id" type="integer">
<generator class="increment" />
</id>
<property name="name" type="string" />

<set name="set" table="A_B">
<key column="a_id"/>
<one-to-many class="B"/>
</set>
</class>
***************************************
public class B {
private int id;
private String email;
private A a;

public A getA() {
return a;
}

public void setA(A a) {
this.a = a;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

}
***********************
<class name="B" table="B" >
<id name="id" column="b_id" type="integer">
<generator class="increment" />
</id>
<property name="email" />
<many-to-one name="a" class="A" column="a_id" />
</class>
**********************************
My test class is as follows

public class Test {

public static void main(String[] args) {
A a = new A();
B b = new B();
B c= new B();
Transaction tx = null;
Session session = null;
a.setName("a");
b.setEmail("bmail");
b.setA(a);
c.setEmail("cmail");
c.setA(a);
a.addSet(b);
a.addSet(c);


try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.save(b);
session.save(c);
session.save(a);
tx.commit();

}
catch (Exception e){
System.out.println(e.getMessage());
}
finally{
session.close();
}
}
}


when I run the build.xml file, I get the following output error
[java] Example Test
[java] INFO - Hibernate 3.1 rc1
[java] INFO - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider, hibernate.max_fetch_depth=1, hibernate.dialect=org.hibernate.dialect.MySQLMyISAMDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.format_sql=true, hibernate.query.substitutions=yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=root, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:mysql://localhost/test, hibernate.show_sql=true, hibernate.connection.password=****, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=1}
[java] INFO - using java.io streams to persist binary types
[java] INFO - using CGLIB reflection optimizer
[java] INFO - using JDK 1.4 java.sql.Timestamp handling
[java] INFO - configuring from resource: /hibernate.cfg.xml
[java] INFO - Configuration resource: /hibernate.cfg.xml
[java] INFO - Reading mappings from resource: A.hbm.xml
[java] INFO - Mapping class: A -> A
[java] INFO - Reading mappings from resource: B.hbm.xml
[java] INFO - Mapping class: B -> B
[java] INFO - Configured SessionFactory: null
[java] INFO - processing extends queue
[java] INFO - processing collection mappings
[java] INFO - Mapping collection: A.set -> B
[java] INFO - processing association property references
[java] INFO - processing foreign key constraints
[java] INFO - Using Hibernate built-in connection pool (not for production use!)
[java] INFO - Hibernate connection pool size: 10
[java] INFO - autocommit mode: false
[java] INFO - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/test
[java] INFO - connection properties: {user=root, password=****}
[java] INFO - RDBMS: MySQL, version: 4.1.14-nt
[java] INFO - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.6 ( $Date: 2004/12/09 15:57:25 $, $Revision: 1.27.4.52 $ )
[java] INFO - Using dialect: org.hibernate.dialect.MySQLDialect
[java] INFO - Using default transaction strategy (direct JDBC transactions)
[java] INFO - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
[java] INFO - Automatic flush during beforeCompletion(): disabled
[java] INFO - Automatic session close at end of transaction: disabled
[java] INFO - JDBC batch size: 15
[java] INFO - JDBC batch updates for versioned data: enabled
[java] INFO - Scrollable result sets: enabled
[java] INFO - JDBC3 getGeneratedKeys(): enabled
[java] INFO - Connection release mode: null
[java] INFO - Maximum outer join fetch depth: 1
[java] INFO - Default batch fetch size: 1
[java] INFO - Generate SQL with comments: disabled
[java] INFO - Order SQL updates by primary key: disabled
[java] INFO - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
[java] INFO - Using ASTQueryTranslatorFactory
[java] INFO - Query language substitutions: {no='N', yes='Y'}
[java] INFO - Second-level cache: enabled
[java] INFO - Query cache: disabled
[java] INFO - Cache provider: org.hibernate.cache.HashtableCacheProvider
[java] INFO - Optimize cache for minimal puts: disabled
[java] INFO - Cache region prefix: hibernate.test
[java] INFO - Structured second-level cache entries: disabled
[java] INFO - Echoing all SQL to stdout
[java] INFO - Statistics: disabled
[java] INFO - Deleted entity synthetic identifier rollback: disabled
[java] INFO - Default entity-mode: POJO
[java] INFO - building session factory
[java] INFO - Not binding factory to JNDI, no JNDI name configured
[java] INFO - Running hbm2ddl schema update
[java] INFO - fetching database metadata
[java] INFO - updating schema
[java] INFO - processing extends queue
[java] INFO - processing collection mappings
[java] INFO - processing association property references
[java] INFO - processing foreign key constraints
[java] INFO - table not found: A
[java] INFO - table not found: B
[java] INFO - table not found: A
[java] INFO - table not found: B
[java] ERROR - Unsuccessful: create table A (a_id integer not null, name varchar(255), primary key (a_id))
[java] ERROR - Can't find file: '.\test\a.frm' (errno: 13)
[java] ERROR - Unsuccessful: alter table B add index FK422D89FA (a_id), add constraint FK422D89FA foreign key (a_id) references A (a_id)
[java] ERROR - Can't create table '.\test\#sql-12c_5e.frm' (errno: 150)
[java] INFO - schema update complete
[java] INFO - cleaning up connection pool: jdbc:mysql://localhost/test
[java] INFO - Checking 0 named queries
[java] Hibernate:
[java] select
[java] max(b_id)
[java] from
[java] B
[java] Hibernate:
[java] select
[java] max(a_id)
[java] from
[java] A
[java] WARN - SQL Error: 1017, SQLState: HY000
[java] ERROR - Can't find file: '.\test\a.frm' (errno: 13)
[java] could not fetch initial value for increment generator
BUILD SUCCESSFUL

I am not sure where the actual problem is.


Top
 Profile  
 
 Post subject: Here is the fix with different example
PostPosted: Tue Nov 08, 2005 12:20 pm 
Newbie

Joined: Thu Oct 06, 2005 9:30 am
Posts: 18
Location: Canada
public class Person {
String firstName;
String lastName;
Set addresses = new HashSet();
long id;

/**
* @param name
* @param name2
*/
public Person(String name, String name2) {
super();
// TODO Auto-generated constructor stub
firstName = name;
lastName = name2;
}

public void addAddress(EmailAddress address){
this.addresses.add(address);
}

/**
* @return Returns the addresses.
*/
public Set getAddresses() {
return addresses;
}
/**
* @param addresses The addresses to set.
*/
public void setAddresses(Set addresses) {
this.addresses = addresses;
}
/**
* @return Returns the firstName.
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName The firstName to set.
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return Returns the id.
*/
public long getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(long id) {
this.id = id;
}
/**
* @return Returns the lastName.
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName The lastName to set.
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}


}

<hibernate-mapping>
<class name="Person" table="people" >
<id name="id" type="long" column="person_id">
<generator class="increment"/>
</id>

<property name="lastName"/>
<property name="firstName"/>

<set name="addresses" table="EMAIL_ADDRESSES" cascade="all" inverse="true" lazy="true">
<key column="person_id"/>
<one-to-many class="EmailAddress" />
</set>

</class>
</hibernate-mapping>


public class EmailAddress {
String userName;
String host;
Person owner;
long id;
/**
* @param host
* @param name
*/
public EmailAddress(String host, String name) {
super();
// TODO Auto-generated constructor stub
this.host = host;
userName = name;
}
/**
* @return Returns the host.
*/
public String getHost() {
return host;
}
/**
* @param host The host to set.
*/
public void setHost(String host) {
this.host = host;
}
/**
* @return Returns the id.
*/
public long getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(long id) {
this.id = id;
}
/**
* @return Returns the owner.
*/
public Person getOwner() {
return owner;
}
/**
* @param owner The owner to set.
*/
public void setOwner(Person owner) {
this.owner = owner;
}
/**
* @return Returns the userName.
*/
public String getUserName() {
return userName;
}
/**
* @param userName The userName to set.
*/
public void setUserName(String userName) {
this.userName = userName;
}


}

<hibernate-mapping>
<class name="EmailAddress" table="EMAIL_ADDRESSES" >
<id name="id" type="long" column="id">
<generator class="increment"/>
</id>

<property name="userName" />
<property name="host" />

<many-to-one name="owner" column="person_id" class="Person" >
</many-to-one>
</class>
</hibernate-mapping>

Following is a test file

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person newPerson = new Person("Foo", "Baar");
EmailAddress bar = new EmailAddress("foo", "foo.aol.com");
EmailAddress cat = new EmailAddress("baar", "Baar@gmail.com");
EmailAddress rat = new EmailAddress("rat", "raat@hotmail.com");
EmailAddress zat = new EmailAddress("zat", "zaat@email.com");
bar.setOwner(newPerson);
cat.setOwner(newPerson);
rat.setOwner(newPerson);
zat.setOwner(newPerson);
newPerson.addAddress(cat);
newPerson.addAddress(bar);
newPerson.addAddress(rat);
newPerson.addAddress(zat);

Transaction tx = null;
Session session = null;




try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();

session.save(newPerson);

tx.commit();

}
catch (Exception e){
System.out.println(e.getMessage());
}
finally{
session.close();
}



}

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 12:29 pm 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
It looks like your mySQL database is FUBARed. Not a Hibernate problem.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.