-->
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.  [ 8 posts ] 
Author Message
 Post subject: Weak Entity
PostPosted: Wed Mar 26, 2008 8:13 pm 
Newbie

Joined: Wed Mar 26, 2008 7:52 pm
Posts: 6
Hi,

I have a Java class named File and another one named FileData. Conceptually these are only one class, but I need this separation because design reasons. The File class has an Id field, but the FileData class hasn't one of its own because it's only extra information for the File class.... so, in the database, FileData would have a foreign key to File that is also its primary key (in other words, FileData is a weak entity that depends on File).

How do i do that? I thought PrimaryKeyJoinColumn would help me, but it didn't...

Regards.


Top
 Profile  
 
 Post subject: Re: Weak Entity
PostPosted: Thu Mar 27, 2008 12:13 am 
Newbie

Joined: Sun Jan 20, 2008 10:26 pm
Posts: 4
raulmt wrote:
Hi,
How do i do that? I thought PrimaryKeyJoinColumn would help me, but it didn't...

Regards.


It would help if you provide some more details - relevant part of the mapping, how exactly it didn't ...

Just a guess- is this what you have?

@OneToOne
@PrimaryKeyJoinColumn
private FileData fileData;


Top
 Profile  
 
 Post subject: Re: Weak Entity
PostPosted: Thu Mar 27, 2008 11:04 am 
Newbie

Joined: Wed Mar 26, 2008 7:52 pm
Posts: 6
michael112 wrote:
raulmt wrote:
Hi,
How do i do that? I thought PrimaryKeyJoinColumn would help me, but it didn't...

Regards.


It would help if you provide some more details - relevant part of the mapping, how exactly it didn't ...

Just a guess- is this what you have?

@OneToOne
@PrimaryKeyJoinColumn
private FileData fileData;


Almost... I put the PrimaryKeyJoinColumn in FileData:

@OneToOne
@PrimaryKeyJoinColumn
private File file;

The problem is that since I don't have an @Id in FileData (because the real primary key must be the column generated by the OneToOne relation) Hibernate says I don't have an Id in this entity.... and I can't put @Id to this line because Hibernate then says it doesn't know how to translate this....

I tried also putting an int attribute ("idx", for example) in FileData with the @Id and in the PrimaryKeyJoinColumn I added the name="idx"... the idx column is created, of course, but it isn't also a foreign key to File->id.... So the problem is I need to generate a column on FileData that is its primary key and also, at the same time, is a foreign key to File->id....

Thanks for any help.


Top
 Profile  
 
 Post subject: Re: Weak Entity
PostPosted: Fri Mar 28, 2008 8:48 pm 
Newbie

Joined: Sun Jan 20, 2008 10:26 pm
Posts: 4
Quote:

Almost... I put the PrimaryKeyJoinColumn in FileData:

@OneToOne
@PrimaryKeyJoinColumn
private File file;

The problem is that since I don't have an @Id in FileData (because the real primary key must be the column generated by the OneToOne relation) Hibernate says I don't have an Id in this entity.... and I can't put @Id to this line because Hibernate then says it doesn't know how to translate this....


You have to put id field in FileData as well but you also have to specify how you want Hibernate to generate values for it:

@Id @GeneratedValue(generator = "myForeignGenerator")
@org.hibernate.annotations.GenericGenerator(
name = "myForeignGenerator",
strategy = "foreign",
parameters = @Parameter(name = "property", value = "file"))
private Long id;

This way it will pick up Id value from the entity property "file" and all will work as expected. You have to assign File property value before you persist FileData but this is what you are after anyway.


Top
 Profile  
 
 Post subject: Re: Weak Entity
PostPosted: Mon Mar 31, 2008 1:51 pm 
Newbie

Joined: Wed Mar 26, 2008 7:52 pm
Posts: 6
michael112 wrote:
You have to put id field in FileData as well but you also have to specify how you want Hibernate to generate values for it:

@Id @GeneratedValue(generator = "myForeignGenerator")
@org.hibernate.annotations.GenericGenerator(
name = "myForeignGenerator",
strategy = "foreign",
parameters = @Parameter(name = "property", value = "file"))
private Long id;

This way it will pick up Id value from the entity property "file" and all will work as expected. You have to assign File property value before you persist FileData but this is what you are after anyway.


That helped me but i still have one issue: I now have a duplicate column in the database. What I want to have in the database is a table like this:

CREATE TABLE `FileData` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_FileData` FOREIGN KEY (`id`) REFERENCES `File` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

and a File table like this:

CREATE TABLE `File` (
`id` int(11) NOT NULL auto_increment,
`contentType` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)


Following your post, I have a FileData class like this:

@Entity
public class FileData {
@Id
@GeneratedValue(generator = "myForeignGenerator")
@GenericGenerator(
name = "myForeignGenerator",
strategy = "foreign",
parameters = @Parameter(name = "property", value = "file"))
private int id;

@Basic(optional=false)
private String name;

@OneToOne()
@JoinColumn()
private File file;
}

But this results in a table like this:
CREATE TABLE `FileData` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`file_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK92C29CB08A7A8DE9` (`file_id`)
CONSTRAINT `FK92C29CB08A7A8DE9` FOREIGN KEY (`file_id`) REFERENCES `File` (`id`)
)

So, I have an Id in FileData that takes its value from the File associated but I have also a file_id (from the OneToOne association) that has of course the same value id column has, and the Foreign key association is on this one, not on the id column....

Can these be done?

Thanks for your help, Michael.


Top
 Profile  
 
 Post subject: Re: Weak Entity
PostPosted: Mon Mar 31, 2008 2:13 pm 
Newbie

Joined: Sun Jan 20, 2008 10:26 pm
Posts: 4
raulmt wrote:
CREATE TABLE `FileData` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_FileData` FOREIGN KEY (`id`) REFERENCES `File` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

and a File table like this:

CREATE TABLE `File` (
`id` int(11) NOT NULL auto_increment,
`contentType` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)


Following your post, I have a FileData class like this:

@Entity
public class FileData {
@Id
@GeneratedValue(generator = "myForeignGenerator")
@GenericGenerator(
name = "myForeignGenerator",
strategy = "foreign",
parameters = @Parameter(name = "property", value = "file"))
private int id;

@Basic(optional=false)
private String name;

@OneToOne()
@JoinColumn()
private File file;
}

Can these be done?

Thanks for your help, Michael.


Looks like it can be done (Except name of the FK constraint), you have to change annotations on top of your File field:
@OneToOne(cascade = CascadeType.ALL, optional=false)
@PrimaryKeyJoinColumn
private File file;

as well as post relevant part of your File annotations - I can't see where file_id is coming from, it must be from some annotation of the File itself.

I'd just map it this way:
@Entity
@Table(name = "File")
public class File
{
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
...

at least it works for me here, just table names are different.


Top
 Profile  
 
 Post subject: Re: Weak Entity
PostPosted: Tue Apr 01, 2008 10:20 am 
Newbie

Joined: Wed Mar 26, 2008 7:52 pm
Posts: 6
michael112 wrote:
Looks like it can be done (Except name of the FK constraint), you have to change annotations on top of your File field:
@OneToOne(cascade = CascadeType.ALL, optional=false)
@PrimaryKeyJoinColumn
private File file;

as well as post relevant part of your File annotations - I can't see where file_id is coming from, it must be from some annotation of the File itself.

I'd just map it this way:
@Entity
@Table(name = "File")
public class File
{
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
...

at least it works for me here, just table names are different.


Adding @PrimaryKeyJoinColumn solves the duplicate column problem.... it is all exactly as I wanted it except the fact that when I generate the database the id column on FileData doesn't have the foreign key constraint.... nevertheless Hibernate seems to work like this constraint exist.... so I'll wait until i finish the database and then I'll add this constraint manually I think.... thank you very much for your help, Michael.


Top
 Profile  
 
 Post subject: Re: Weak Entity
PostPosted: Thu Apr 24, 2008 7:10 pm 
Newbie

Joined: Wed Mar 26, 2008 7:52 pm
Posts: 6
I was missing something in michael solution.... the optional=false in the OneToOne annotation for private File file; .... without that, the behaviour is the same, but hibernate doesn't add the constraint, what it is perfectly reasonable.

Thanks Michael, again, for your help.

Regards.


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