-->
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.  [ 13 posts ] 
Author Message
 Post subject: Hibernate mapping
PostPosted: Tue Jul 22, 2008 5:47 pm 
Newbie

Joined: Tue Jul 22, 2008 5:43 pm
Posts: 6
Hello,
I need to know exactly when do we use te one to one, many to one, many to many and one to many mappings.

kindly reply me.

i will be grateful

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

[b]Hibernate version:[/b]

[b]Mapping documents:[/b]

[b]Code between sessionFactory.openSession() and session.close():[/b]

[b]Full stack trace of any exception that occurs:[/b]

[b]Name and version of the database you are using:[/b]

[b]The generated SQL (show_sql=true):[/b]

[b]Debug level Hibernate log excerpt:[/b]


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 23, 2008 7:46 am 
Newbie

Joined: Wed Jul 23, 2008 2:50 am
Posts: 16
it depends on your database-schema and what do you want to do that's all
read more about database, data modeling concept.....


Top
 Profile  
 
 Post subject: hibernate mapping
PostPosted: Wed Jul 23, 2008 11:21 am 
Newbie

Joined: Tue Jul 22, 2008 5:43 pm
Posts: 6
hi larslars,

Actually i want to create i database but i dont know how to use the mappings for my tables i mean if its many to many mapping which table will have the forign key , and similarly for the one to many, many to one and one to one mappings,
can you please guide me for mappings.

I shall be grateful to u


Top
 Profile  
 
 Post subject: Generate the DDL
PostPosted: Wed Jul 23, 2008 7:05 pm 
Newbie

Joined: Tue Dec 07, 2004 8:15 pm
Posts: 10
Location: Adelaide Australia
Getting the mappings right can be tricky but once you have what you want you can use the Hibernate hbm2ddl tool to generate your database DDL with appropriate foreign key constraints or just set the hibernate.hbm2ddl.auto property to "create" or "update" in your hibernate config to create the schema for you (make sure your DBA thinks this is OK though! :) ) .

It would be worth picking up a book on basic database design to help you along, this is all fairly basic stuff. I would recommend one but it was 20 years ago when I learned this so I couldn't recommend anything current. perhaps someone else could suggest something.

_________________
Peter Kelley


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 24, 2008 12:25 am 
Newbie

Joined: Thu Mar 17, 2005 2:06 pm
Posts: 12
If you have already written your Java code then you can use a tool to analyze it and figure out the relationships, mappings, one-to-one, one-to-many, etc.

I tried this tool and it works pretty good: http://www.javatosql.com it will generate sql too if you need it.


Top
 Profile  
 
 Post subject: Re: hibernate mapping
PostPosted: Thu Jul 24, 2008 3:33 am 
Newbie

Joined: Wed Jul 23, 2008 2:50 am
Posts: 16
duniya wrote:
hi larslars,

Actually i want to create i database but i dont know how to use the mappings for my tables i mean if its many to many mapping which table will have the forign key , and similarly for the one to many, many to one and one to one mappings,
can you please guide me for mappings.

I shall be grateful to u


dunia!

one-to-many, one-to-one, and so on, are relations between your tables and it is you, that definde the relations.
for example if you have a table that describe your Customers and the other tabale , you describe order, there is one-to-many relationship here and you can use the one-to-many mapping. ok?,
and what means one-to-many relationship, its mean that every Customer can have many orders and each order have just only one Customer.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 24, 2008 11:24 am 
Newbie

Joined: Thu Mar 17, 2005 2:06 pm
Posts: 12
Another way to think about it is from your Java code, which I feel is more intuitive.

Using the sample above, if you have classes Customer and Order:

Code:
public class Customer {

     private Set<Order> order;

     private String name, address, etc.

}

public class Order {

     private Customer customer;

     private double cost;

}


You know that a Customer can have many Orders, because Customer has a Set of Orders. So that would be a one-to-many from Customer to Order.

You know that an Order can belong to only one Customer, because Order has single reference to Customer (not a Set). So that would be a many-to-one from Order to Customer.


Top
 Profile  
 
 Post subject: Hibernate problem
PostPosted: Fri Jul 25, 2008 12:09 pm 
Newbie

Joined: Tue Jul 22, 2008 5:43 pm
Posts: 6
Hi larslars,

i have to make a database like this

Role User Company Job jobseekerdata

These are four entities.
users can be a representative of company or a jobseeker. if its a representative of company then it will be able to post jobs. If he is a jobseeker his data will be stored in the jobseekersdata and he will be able to apply for a job.


Can you please help me in creating this database?

I will be grateful to you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 5:22 pm 
Newbie

Joined: Thu Mar 17, 2005 2:06 pm
Posts: 12
Hi Duniya,

I've create 4 Java objects to represent your schema. I've also allowed User to be able to be a JobSeeker AND a Company, in case that user wants to list jobs and apply for jobs.

I'm sure there are more properties you'd like to add to the classes, and getters and setters. Do that, then all you need to do is compile this code and jar it up. Upload the JAR file at http://www.javatosql.com and it will email you SQL for your database and hibernate mapping files.

Code:
public class User {

   private String name;
   
   private String loginId;
   
   private String password;
   
   private String email;
   
   // Null if this user is not a company
   private Company company;
   
   // Null if this user is not a jobSeeker
   private JobSeeker jobSeeker;

}

public class Company {

   private String name;
   
   private User user;
   
}

public class JobSeeker {
   
   private User user;
   
}

public class Job {

   private Company company;
   
   private String title;
   
   private String description;

}


Top
 Profile  
 
 Post subject: hibernate mappng
PostPosted: Sun Jul 27, 2008 12:59 pm 
Newbie

Joined: Tue Jul 22, 2008 5:43 pm
Posts: 6
Hi Larslars and newbi,


If i am creating an other table called roles.
Then do i need to create user instance in the company table and jobseeker table.

And can you please tell me how to represent the many to many
one to one
many to one
one to many in the xml file for mapping the classes

I shall be grateful.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 27, 2008 7:51 pm 
Newbie

Joined: Thu Mar 17, 2005 2:06 pm
Posts: 12
Duniya,

I added a class Roles and compiled the sample code I posted. I put the class files in a JAR and uploaded at the tool I mentioned - http://www.javatosql.com. Here are the results of the sql and some hibernate mappings so you can see how some of the relationships are mapped. If you want to see all the relationships, you can do the same thing as I described above.

SQL:

Code:
CREATE TABLE COMPANY
(
   ID INTEGER NOT NULL AUTO_INCREMENT,
   NAME VARCHAR(255) NOT NULL,
   USER_ID_USER INTEGER NOT NULL,
   PRIMARY KEY(ID)) engine=InnoDB;

CREATE TABLE JOB
(
   ID INTEGER NOT NULL AUTO_INCREMENT,
   DESCRIPTION VARCHAR(255) NOT NULL,
   TITLE VARCHAR(255) NOT NULL,
   COMPANY_ID INTEGER NOT NULL,
   PRIMARY KEY(ID)) engine=InnoDB;

CREATE TABLE JOB_SEEKER
(
   ID INTEGER NOT NULL AUTO_INCREMENT,
   USER_ID_USER INTEGER NOT NULL,
   PRIMARY KEY(ID)) engine=InnoDB;

CREATE TABLE ROLES
(
   ID INTEGER NOT NULL AUTO_INCREMENT,
   USER_ID INTEGER NOT NULL,
   PRIMARY KEY(ID)) engine=InnoDB;

CREATE TABLE USER
(
   ID INTEGER NOT NULL AUTO_INCREMENT,
   EMAIL VARCHAR(255) NOT NULL,
   LOGIN_ID VARCHAR(255) NOT NULL,
   NAME VARCHAR(255) NOT NULL,
   PASSWORD VARCHAR(255) NOT NULL,
   PRIMARY KEY(ID)) engine=InnoDB;

ALTER TABLE COMPANY
   ADD CONSTRAINT COMPANY_FK_USER_ID_USER
   FOREIGN KEY (USER_ID_USER)
   REFERENCES USER (ID)
   ON DELETE CASCADE
;

ALTER TABLE JOB
   ADD CONSTRAINT JOB_FK_COMPANY_ID
   FOREIGN KEY (COMPANY_ID)
   REFERENCES COMPANY (ID)
   ON DELETE CASCADE
;

ALTER TABLE JOB_SEEKER
   ADD CONSTRAINT JOB_SEEKER_FK_USER_ID_USER
   FOREIGN KEY (USER_ID_USER)
   REFERENCES USER (ID)
   ON DELETE CASCADE
;

ALTER TABLE ROLES
   ADD CONSTRAINT ROLES_FK_USER_ID
   FOREIGN KEY (USER_ID)
   REFERENCES USER (ID)
   ON DELETE CASCADE
;


MAPPING FOR COMPANY:

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="Company" table="COMPANY">
        <id column="ID" name="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property length="255" name="name" not-null="true" type="java.lang.String">
            <column name="NAME" sql-type="VARCHAR"/>
        </property>
        <many-to-one class="User" name="user" not-null="true">
            <column name="USER_ID_USER"/>
        </many-to-one>
    </class>
</hibernate-mapping>


MAPPING FOR USER:
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="User" table="USER">
        <id column="ID" name="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property length="255" name="email" not-null="true" type="java.lang.String">
            <column name="EMAIL" sql-type="VARCHAR"/>
        </property>
        <property length="255" name="loginId" not-null="true" type="java.lang.String">
            <column name="LOGIN_ID" sql-type="VARCHAR"/>
        </property>
        <property length="255" name="name" not-null="true" type="java.lang.String">
            <column name="NAME" sql-type="VARCHAR"/>
        </property>
        <property length="255" name="password" not-null="true" type="java.lang.String">
            <column name="PASSWORD" sql-type="VARCHAR"/>
        </property>
        <one-to-one class="Company" name="company"/>
        <one-to-one class="JobSeeker" name="jobSeeker"/>
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: hibernate mapping
PostPosted: Mon Jul 28, 2008 10:01 am 
Newbie

Joined: Tue Jul 22, 2008 5:43 pm
Posts: 6
hi ,

If the user to company is a many to one relation ship then which XML file should contain the many to one element?

Thanks


Top
 Profile  
 
 Post subject: wikcet webpages
PostPosted: Thu Jul 31, 2008 5:09 pm 
Newbie

Joined: Tue Jul 22, 2008 5:43 pm
Posts: 6
Hi Newbie and larslars,

I have created te database with ur kind help . Now i want to create the webpages for the jobseekers to search for a job and apply for a job.


Can you please help me in this regard?

thanks


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