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.  [ 4 posts ] 
Author Message
 Post subject: Hi can any one give simple example of May-to-one code
PostPosted: Tue May 27, 2008 3:10 am 
Newbie

Joined: Wed Mar 12, 2008 11:16 am
Posts: 15
Hi experts
I m new to hibernate
can any one give complete code for many to one relation.
I want the code of insertion in to a table having foreign key reference with other table primary key.
Plz Plz Plz Plz Plz Plz Plz Plz Plz Plz Plz Plzhelp me.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 3:18 am 
Newbie

Joined: Fri May 23, 2008 5:20 am
Posts: 11
Location: Minusio, Switzerland
Message (this is in italian)

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.zattix.Messaggio" table="messaggi">
        <id name="id" type="int">
            <generator class="native" />
        </id>
        <many-to-one name="mittente" class="com.zattix.Utente" column="id_mittente" />
        <many-to-one name="destinatario" class="com.zattix.Utente" column="id_destinatario" fetch="select" />
        <property name="testo" type="text" />
        <property name="data" type="calendar" />
        <property name="eliminato" type="boolean" />
    </class>
</hibernate-mapping>



and User. User contain a List of Message
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.zattix.Utente" table="utenti">
        <id name="id" type="int" column="idUtente">
            <generator class="native" />
        </id>
        <property name="username" type="string" length="15" />
        <property name="nome" type="string" length="30" />
        <property name="cognome" type="string" length="30" />
        <property name="password" type="string" length="40" />
        <property name="sesso" type="string" length="1" />
        <property name="data_registrazione" type="calendar" />
        <property name="data_nascita" type="calendar" />
        <property name="hash" type="string" length="40" />
        <property name="verificato" type="int" length="1" />
        <property name="immagine" type="string" length="45" />
        <many-to-one name="lingua" class="com.zattix.Lingua" />
        <many-to-one name="nazione" class="com.zattix.Nazione" />
        <many-to-one name="regione" class="com.zattix.Regione" />
        <many-to-one name="localita" class="com.zattix.Localita" />
        <bag name="accessi" table="UtentiAccessi" lazy="true">
            <key column="idUtente"/>
            <many-to-many column="idLogin" class="com.zattix.Login"/>
        </bag>
       
        <bag name="messaggi" cascade="all" inverse="true" lazy="true" order-by="data desc">
            <key column="id_destinatario"/>
            <one-to-many class="com.zattix.Messaggio" />
        </bag>

    </class>
</hibernate-mapping>





I hope to have you helped

_________________
- ianaz -

http://www.ianaz.ch


Top
 Profile  
 
 Post subject: thank u very much can plz send some other example in english
PostPosted: Tue May 27, 2008 4:33 am 
Newbie

Joined: Wed Mar 12, 2008 11:16 am
Posts: 15
thank u very much can plz send some other example in english


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 10:11 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Here's a simple one-to-many and many-to-one bi-directional relationship mapped with Hibernate and the Java Persistence API (JPA) annotations.

Image

Code:
import javax.persistence.*;
@Entity
public class Player {
private long id;
private String nickName;
private Team team;

@ManyToOne
@JoinColumn(name="team_id")     
public Team getTeam() {return team;}
public void setTeam(Team t) {this.team = t;}
@Id
@GeneratedValue
public long getId() {return id;}
public void setId(long id) {this.id = id;}
public String getNickName() {return nickName;}
public void setNickName(String n) {nickName = n;}
}



Code:
import java.util.List; import javax.persistence.*;
@Entity
public class Team {
private long id;
private String name;
private List<Player> players;

@OneToMany(mappedBy="team", 
              targetEntity=Player.class,
    fetch=FetchType.EAGER, cascade = CascadeType.ALL)
public List<Player> getPlayers() {return players;}
public void setPlayers(List<Player> p){players=p;}
@Id
@GeneratedValue
public long getId() {return id;}
public void setId(long id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}


Using the code in an application would look something like this:

Code:
public static void main(String args[]){
    HibernateUtil.recreateDatabase();
    Session session=HibernateUtil.beginTransaction();

    Team team = new Team();
    Player p1 = new Player();
    Player p2 = new Player();

    session.save(team);
    session.save(p1);
    session.save(p2);

    team.setName("Pickering Atoms");
    p1.setNickName("Lefty");
    p1.setTeam(team);
    p2.setNickName("Blinky");
    p2.setTeam(team);
    HibernateUtil.commitTransaction();
  }


The full free tutorial on Hibernate and JPA mappings can be found here:

http://www.thebookonhibernate.com/HiberBookWeb/learn.jsp?tutorial=18mappingonetomanyassociations

Good luck!

_________________
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.  [ 4 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.