-->
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: A cure for excessive normalisation? One class many tables
PostPosted: Fri May 20, 2011 2:25 pm 
Newbie

Joined: Thu May 19, 2011 6:42 am
Posts: 2
I am mapping to a schema with the Following tables

Table Fuel
Columns
ID
Name
Fuel_Type_ID (which is an FK)

Table Fuel_Type
Columns
ID
Name

I want to have a single class

public class Fuel{
private Long id;
private String name;
private String fuelType;

.... accessors omitted
}

I have looked at using the <join/> element but the <key/> seems to refer from the Fuel_Type to the Fuel ID but we don't have that association. Is there a way to denormalise this scenario by loading the fuel.

I.e the equivalent of this

Select f.*, ft.Name as FuelType from f Fuel, ft FuelType where f.Fuel_Type_ID = ft.ID;

By the way I want to achieve this with hbm.xml not attributes.

Any help much appreciated.


Top
 Profile  
 
 Post subject: Re: A cure for excessive normalisation? One class many tables
PostPosted: Mon May 23, 2011 4:34 pm 
Beginner
Beginner

Joined: Mon Jul 28, 2008 4:36 pm
Posts: 24
You are asking, "How can I do regular SQL in Hibernate" but I'm answering, "How can I use Hibernate to do with Java objects what I used to do with SQL."
Code:
org.hibernate.Session hS = HibernateUtil.getSessionFactory().getCurrentSession();
try {
    hS.beginTransaction();
    Fuel fuel = (Fuel) hS.get(Fuel.class, fuelId);
    System.out.println(fuel.getName());
    System.out.println(fuel.getFuelType().getName());
} finally {
    if (hS.isOpen()) {
        hS.close();
    }
}

I recommend getting one of the books and reading it. There are lots of good examples, plus a certain amount of manifesto that gives you an idea what Hibernate is doing and why. I have Hibernate In Action because I'm old. It seems every page of this forum advertises Java Persistence with Hibernate at the top of the page - that's probably worth looking into.


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.