Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: one to one connection generates SET?
PostPosted: Tue Mar 29, 2011 4:43 am 
Newbie

Joined: Wed Mar 16, 2011 8:32 am
Posts: 4
Hi!

I have been fighting with this problem now two days and I am done, head is empty and I can't continue. I am using MySQLWorkbench to model a database, so I get SQL-scripts. After that I am using Hibernate tool in Eclipse to generate xml and java codes.

Problem is that I have ADDRESS-table, COMPANY and BRANCH. I would like to make that kind of structure that company can have one address and many branches. Every Branch can have one address too. No I have one to one connection between COMPANY and ADDRESS and one-to-many connection between COMPANY and BRANCH. SQL script is ok, but when I am using Hibernatetool I get every time errormessage lika that:

org.hibernate.MappingException: Foreign key (FK754219A212F4C577:BRANCH [COMPANY_companyID])) must have same number of columns as the referenced primary key (COMPANY [companyID,ADDRESS_addressId])
Foreign key (FK754219A212F4C577:BRANCH [COMPANY_companyID])) must have same number of columns as the referenced primary key (COMPANY [companyID,ADDRESS_addressId])
<No message>


If I correct this like changing COMPANY tables AddressId not identifying (FK) I get strange code:


* Address generated by hbm2java
*/
public class Address implements java.io.Serializable {

private int addressId;
private String name;
private String address1;
private String address2;
private String zip;
private String city;
private String country;
private Set emails = new HashSet(0);
private Set phoneses = new HashSet(0);
*******private Set companies = new HashSet(0);*********


How I can get a SET even if I have one to one connection?? How I should implement that to avoid that kind of problems, pls help?



QUESTION 2

Same table ADDRESS has one to many connection to EMAILS table. Why Hibernate tool generates Address attribute under Email-class? There is Set of emails in Address object like it should be, but why there is Address in Email object?

public class Email implements java.io.Serializable {

private EmailId id;
***private Address address;****?
private Integer type;


Sorry for my English, hope someone could help me :)
Sami


Top
 Profile  
 
 Post subject: SQL scripts are here
PostPosted: Tue Mar 29, 2011 4:57 am 
Newbie

Joined: Wed Mar 16, 2011 8:32 am
Posts: 4
DROP TABLE IF EXISTS `taikaDB`.`ADDRESS` ;

CREATE TABLE IF NOT EXISTS `taikaDB`.`ADDRESS` (
`addressId` INT(11) NOT NULL ,
`name` VARCHAR(45) NULL ,
`address1` VARCHAR(45) NULL COMMENT ' ' ,
`address2` VARCHAR(45) NULL ,
`zip` VARCHAR(45) NULL ,
`city` VARCHAR(45) NULL ,
`country` VARCHAR(45) NULL ,
PRIMARY KEY (`addressId`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `taikaDB`.`EMAIL`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `taikaDB`.`EMAIL` ;

CREATE TABLE IF NOT EXISTS `taikaDB`.`EMAIL` (
`emailAddress` VARCHAR(45) NOT NULL ,
`ADDRESS_addressId` INT(11) NOT NULL ,
`type` INT(11) NULL ,
PRIMARY KEY (`emailAddress`, `ADDRESS_addressId`) ,
INDEX `fk_EMAIL_ADDRESS1` (`ADDRESS_addressId` ASC) ,
CONSTRAINT `fk_EMAIL_ADDRESS1`
FOREIGN KEY (`ADDRESS_addressId` )
REFERENCES `taikaDB`.`ADDRESS` (`addressId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `taikaDB`.`COMPANY`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `taikaDB`.`COMPANY` ;

CREATE TABLE IF NOT EXISTS `taikaDB`.`COMPANY` (
`companyID` INT(11) NOT NULL ,
`ADDRESS_addressId` INT(11) NOT NULL ,
PRIMARY KEY (`companyID`) ,
INDEX `fk_COMPANY_ADDRESS1` (`ADDRESS_addressId` ASC) ,
CONSTRAINT `fk_COMPANY_ADDRESS1`
FOREIGN KEY (`ADDRESS_addressId` )
REFERENCES `taikaDB`.`ADDRESS` (`addressId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `taikaDB`.`BRANCH`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `taikaDB`.`BRANCH` ;

CREATE TABLE IF NOT EXISTS `taikaDB`.`BRANCH` (
`branchId` INT(11) NOT NULL ,
`COMPANY_companyID` INT(11) NOT NULL ,
PRIMARY KEY (`branchId`, `COMPANY_companyID`) ,
INDEX `fk_BRANCH_COMPANY1` (`COMPANY_companyID` ASC) ,
CONSTRAINT `fk_BRANCH_COMPANY1`
FOREIGN KEY (`COMPANY_companyID` )
REFERENCES `taikaDB`.`COMPANY` (`companyID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;


Top
 Profile  
 
 Post subject: Re: one to one connection generates SET?
PostPosted: Sun Apr 10, 2011 3:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
You model says there is a one to many relationship. Thus that is a set on one side and a single association on the other side. The generated code exactly reflect your model.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: one to one connection generates SET?
PostPosted: Sun Apr 10, 2011 3:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
Sorry missed it was pointing to the primary key.

This should actually work since http://opensource.atlassian.com/project ... se/HBX-524

What version are you using.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: one to one connection generates SET?
PostPosted: Sun Apr 10, 2011 3:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
Ah, you haven't marked the company address id as unique. As long as that isn't there my first post is correct. There are multiple companies that can point to the same adress.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 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.