-->
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.  [ 4 posts ] 
Author Message
 Post subject: schemaexport anf teh size of foreign keys
PostPosted: Sat Nov 15, 2003 11:32 pm 
Newbie

Joined: Sat Nov 15, 2003 11:24 pm
Posts: 5
Hi,

I use shemaexport to generate my sql schema (mysql dialect).
All my keys are VARCHAR(32). Yet, when schemaexport generates a foreign key, it uses VARCHAR(255).

This is an example of what schemaexport generates:

create table Department (
id char(32) not null,
name VARCHAR(40) )

create table Employee (
id char(32) not null,
firstName VARCHAR(40)
firstName VARCHAR(40)
departmentId VARCHAR(255))
alter table Employee add index (departmentId), add constraint FK9F084418F1DA02D foreign key (departmentId) references Department (id);

I would expect Employee.departmentId to be VARCHAR(32) to match Department.Id;
Same thing for composite keys in many-to-many tables.

Is there a way to make schemaexport generate the right foreign keys or do I have to keep on editing manually the resulting file after every data model modification?

Thanks,

-Vincent.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 15, 2003 11:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Don't believe you. What does you mapping document look like? What version of Hibernate is it?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 16, 2003 6:42 pm 
Newbie

Joined: Sat Nov 15, 2003 11:24 pm
Posts: 5
Hi Gavin,


I use Hibernate 2.0.3.

Here is the code:

package test.schemaexport;

public class Department {
private String id = null;
private String name;

public String getId() { return id; }
public void setId(String id) { this.id = id;}
public String getName(){return name;}
public void setName(String name){this.name = name;}
}

package test.schemaexport;

public class Employee{
String id = null;
String name = null;
Department department =null;

public String getId(){return id;}
public void setId(String id){ this.id = id;}
public String getName() { return name;}
public void setName(String name) { this.name = name;}
public Department getDepartment(){return department;}
public void setDepartment(Department department){this.department = department;}
}


Here are the mapping files:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="test.schemaexport.Department" table="department">
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="test.schemaexport.Employee" table="employee">
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name"/>
<many-to-one name="department" column="departmentId" not-null="true"/>
</class>
</hibernate-mapping>

schema export produces the following script file:

drop table employee;
drop table department;
create table employee (
id char(32) not null,
name VARCHAR(255),
departmentId VARCHAR(255) not null,
primary key (id)
);
create table department (
id char(32) not null,
name VARCHAR(255),
primary key (id)
);
alter table employee add index (departmentId), add constraint FK4722E6AEC80AC00D foreign key (departmentId) references department (id);


I use the following ant task:
<schemaexport
properties="${web-inf}/classes/hibernate.properties"
quiet="no"
text="yes"
drop="no"
delimiter=";"
output="./scripts/test.sql">
<fileset dir="${src}/test/schemaexport">
<include name="**/*.hbm.xml"/>
</fileset>
</schemaexport>


-Vincent.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 16, 2003 10:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This happened because you overrode the SQL type mapping using an sql-type attribute. If you would have just used length="32", it would work. Otherwise, if you insist that you need CHAR, not VARCHAR, you'll have to use a <column> element for the foreign keys also.


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