-->
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: How mapping this relation with annotations
PostPosted: Mon Jun 30, 2008 8:19 am 
Newbie

Joined: Fri Feb 08, 2008 7:14 am
Posts: 4
Table A
id_a int,
name varchar(30),
primary key(id_a)

Table B
id_b int,
name varchar(30),
primary key(id_b)

Table Version
id_version int,
discriminator char(1) in ('A','B'),
id_entity int,
primary key(id_version, discriminator, id_entity)

Table Version holds versions of a's and b's items in column id_entity. Each class (a and b) have a list of versions.

Table A
id_a name
1 'aaaa'
2 'aaa'

Table B
id_b name
1 'bbbbbb'
3 'bb'

Version
id_version discriminator id_entity
1 'A' 1
2 'A' 2
3 'B' 1
4 'B' 3


I am test whit @Any but don't work.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 10:51 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Here are a number of tutorials on how to map associations with Hibernate and Java Persistence API Annotations (JPA).

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=19mappingmanytomanyrelationships

You can do very complex things with annotations. I map this complex relationship to show just what can be done:

Image

Code:
package com.examscam.model;
  import java.util.*;import javax.persistence.*;
  @Entity
  @Table(name = "client", schema = "examscam")
  public class Client {
  private List<Address> addresses = new Vector<Address>();
  private List<Skill> skills = new Vector<Skill>();
  private ClientDetail clientDetail;
  private Long id;private String username;
  private String password;private Boolean verified;
  @Id
  @GeneratedValue
  @Column(name = "id")
  public Long getId() {return id;}
  public void setId(Long id) {this.id = id;}
  @ManyToMany
  @JoinTable(name = "client_skill",
  joinColumns = { @JoinColumn(name = "client_id") },
  inverseJoinColumns = { @JoinColumn(name = "skill_id") })
  public List<Skill> getSkills() {return skills;}
  public void setSkills(List<Skill> skills){this.skills=skills;}   
  @OneToMany(mappedBy="client", targetEntity=Address.class,   
  fetch=FetchType.EAGER, cascade = CascadeType.ALL)
  public List<Address> getAddresses() {return addresses;}
  public void setAddresses(List<Address> addresses) {
  this.addresses = addresses;
  }
  @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
  @JoinColumn(name="detail_id")
  public ClientDetail getClientDetail(){return clientDetail;}
  public void setClientDetail(ClientDetail clientDetail) {
  this.clientDetail = clientDetail;
  }
  public String getPassword() {return password;}
  public void setPassword(String password){this.password = password;}
  public String getUsername() {return username;}
  public void setUsername(String username) {
  this.username = username;
  }
  public Boolean getVerified() {return verified;}
  public void setVerified(Boolean verified){this.verified=verified;}
  }
 



Image

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.