-->
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.  [ 2 posts ] 
Author Message
 Post subject: Creating a mapping with dual columns referencing same table
PostPosted: Fri Mar 21, 2008 12:10 pm 
Newbie

Joined: Fri Mar 21, 2008 11:35 am
Posts: 10
Hibernate version: 3.x - JPA support

Mapping documents:

I'm wondering if someone could give me a little advice on this situation.

I have a table, Users, that I defined as the following in Java:

Code:
    private Long id;
    private String username;
    private String password;

    private Date creationDate;
    private String emailAddress;

    private List<SecurityGroup> securityGroups;
    private List<UserProperty> userProperties;
    private List<UserConnection> myConnections;
    private List<UserConnection> joinConnections;
   
    private Profile profile;


And the other relevant structure is the UserConnection class:

Code:
   private Long id;
   private Date createdOn;
   private UserConnectionType connectionType;
   private User initiatingUser;
   private User targetUser;


What I'm trying to generate, as you can probably guess, is a left side/right side referencing structure for showing user to user relationships. I'm using initiating user and target user as a way to track who created the relationship (initiating user). It should also be usable in parent/child structure (ie a Mother has a child..)

so I tried decorating the code with the following JoinTable annotation. I'm generating the schema from the Hibernate mappings using hbm2ddl, so my assumption is that my annotations are correct as long as it creates the schema that I expect.

Code:
@JoinTable(name="users",      joinColumns=@JoinColumn(name="initiating_user",referencedColumnName="id"))


What I'm essentially expecting is that no new column is added to the users table, and a FK is created from user_connections.initiating_user to users.id. Instead, I'm getting a new column in the users table, and no column in the user_connections table.

I'm not really sure what I'm doing wrong with my mapping, if anyone has any comments what I've done wrong it would help me quite a bit.

Thanks,

John


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 21, 2008 8:17 pm 
Newbie

Joined: Fri Mar 21, 2008 11:35 am
Posts: 10
It looks like I was over analyzing the issue, so I tried something simpler.

I decorated the classes with the following annotations, the schema generates properly with FK's.

In User.java:

Code:
   @OneToMany(cascade={CascadeType.ALL}, fetch = FetchType.LAZY, mappedBy="targetUser")
    public List<UserConnection> getJoinConnections()
    {
       return joinConnections;
    }

    @OneToMany(cascade={CascadeType.ALL}, fetch = FetchType.LAZY, mappedBy="initiatingUser")
    public List<UserConnection> getMyConnections()
    {
       return myConnections;
    }


In UserConnections.java:

Code:
   @ManyToOne @JoinColumn(name="initiating_user_id")
   public User getInitiatingUser() {
      return initiatingUser;
   }

   @ManyToOne @JoinColumn(name="target_user_id")
   public User getTargetUser() {
      return targetUser;
   }


Figured I'd post the solution, in case anyone else had similar problems.


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