-->
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.  [ 7 posts ] 
Author Message
 Post subject: Multiple associations on single column with annotations
PostPosted: Tue Nov 24, 2009 5:45 am 
Newbie

Joined: Thu Sep 24, 2009 12:46 am
Posts: 10
Hi,

I have 3 tables say table A {id,..}, table B{Aid,..}, table C{Aid,..}
Aid in table b and c foreign key references to table A primary key id.

1. A have one-to-one relation with B on Aid.
2. A have one-to-many association with C on Aid.

I need to define the POJO's using annotations

public class A
{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "FL_ID" )
private Integer id;
}


public class B
{
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private FlHdr flId;
}

public class C
{
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "ID")
private Set<A> id;
}

I was confused how to configure in Class A for Id property, is i can mention 2 associations on id column.
guide me is how to do it for class A(i think Class b and c associations are correct)

Thanks,


Top
 Profile  
 
 Post subject: Re: Multiple associations on single column with annotations
PostPosted: Tue Nov 24, 2009 5:54 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
There is no problem when two foreign keys reference the same primary key. Thats what foreign keys are for. I hope these are not your complete mappings(?) as you need Ids in B and C. Also, your class C seems to be very weird. Why do you call the foreign key column to a "ID"?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Multiple associations on single column with annotations
PostPosted: Tue Nov 24, 2009 6:34 am 
Newbie

Joined: Thu Sep 24, 2009 12:46 am
Posts: 10
public class A
{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "ID")
@OneToMany(cascade = CascadeType.ALL)
@OneToOne (mappedBy = id)
private Integer id;
..
}


public class B
{
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private FlHdr aId;
...
}

public class C
{
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "AID")
private Set<A> aId;
...
}
Is the above code is right..?

This is my complete mapping association between the Classes. (which is already mentioned on first post)
1. A have one-to-one relation with B on Aid.
2. A have one-to-many association with C on Aid.
"These associations needs to bi-directional"

The Tables B and c does not have primary keys.


Top
 Profile  
 
 Post subject: Re: Multiple associations on single column with annotations
PostPosted: Tue Nov 24, 2009 6:50 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
you always need ids on entities (which is already mentioned on my first post), so your mapping is wrong.

Please read the manual.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Multiple associations on single column with annotations
PostPosted: Tue Nov 24, 2009 7:27 am 
Newbie

Joined: Thu Sep 24, 2009 12:46 am
Posts: 10
ok.
I can make aid as @Id column in Class B and another variable as @Id column in the Class C

then the code is ok.

public class A
{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "ID")
@OneToMany(cascade = CascadeType.ALL)
@OneToOne (mappedBy = id)
private Integer id;
..
}


public class B
{
@Id
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private FlHdr aId;
...
}

public class C
{
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "AID")
private Set<A> aId;

@Id
private String vId
...
}


Top
 Profile  
 
 Post subject: Re: Multiple associations on single column with annotations
PostPosted: Tue Nov 24, 2009 7:38 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Please read the complete manual, especially the part about mapping associations.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Multiple associations on single column with annotations
PostPosted: Fri Nov 27, 2009 7:54 am 
Newbie

Joined: Thu Sep 24, 2009 12:46 am
Posts: 10
Hi,

I have been created the following files for my logic

Code:
public class A
{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "ID")
@OneToMany(cascade = CascadeType.ALL)
@OneToOne (mappedBy = "id")
private Integer id;
..
}


public class B
{
@Id
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private A aId;
...
}



With the above code I am getting error
org.hibernate.MappingException: Could not determine type for: com.sample.common.to.A, at table: B, for columns: [org.hibernate.mapping.Column(id)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:269)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
at org.hibernate.mapping.RootClass.validate(RootClass.java:193)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1108)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1293)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
at com.capgent.cpt.service.database.utils.HibernateDatabaseCalls.getDataSource(HibernateDatabaseCalls.java:389)
at com.capgent.cpt.service.database.utils.HibernateDatabaseCalls.executeHql(HibernateDatabaseCalls.java:866)
at com.capgent.cpt.service.database.utils.HibernateDatabaseCalls.testHQL(HibernateDatabaseCalls.java:748)
at com.capgent.cpt.service.database.utils.HibernateDatabaseCalls.main(HibernateDatabaseCalls.java:1229)

if i delete @id from the aId property it works fine, is @Id does not work for the Collection and Object(Pojo) type..


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