-->
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: [newbie] one-to-one annotation mapping
PostPosted: Wed Apr 06, 2011 2:39 pm 
Newbie

Joined: Wed Apr 06, 2011 2:12 pm
Posts: 1
Hi. I have two MySQL tables AutoCards and Cities. AutoCard has CityID field which should be connected with CityID field of Cities. The goal is to get city name via AutoCard bean.

Here is AutoCard.java:

Code:
package vac.entities;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.OneToOne;
import javax.persistence.OneToMany;
import javax.persistence.ManyToOne;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import vac.entities.City;

@Entity
@Table(name = "AutoCards")
public class AutoCard implements Serializable
{
   @Id
   @GeneratedValue
   private Long id;

   @OneToOne
   private City city;

   public City getCity() {
      return city;
   }

   public void setCity(City city) {
      this.city = city;
   }

   public String getCityName()
   {
      getCity().setId(getCityID());
      return getCity().getCityName();
   }

   ...
   
   @Column(name = "CityID")
   private Integer cityID;

   ...getters and setters...
}

and City.java:
Code:
package vac.entities;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Cities")
public class City implements Serializable
{
   @Id
   @GeneratedValue
   private Integer id;
   
   @Column(name = "CityName")
   private String cityName;
   
   @Column(name = "Visible")
   private Integer visible;

   ...etc...
}


When I call getCityName method, Hibernate write next error message:
Code:
java.sql.SQLException: Unknown column 'autocard0_.city_id' in 'field list'
   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)
   com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)
   ....


Help me please! :)


Top
 Profile  
 
 Post subject: Re: [newbie] one-to-one annotation mapping
PostPosted: Wed Sep 12, 2012 1:55 am 
Newbie

Joined: Wed Sep 12, 2012 1:53 am
Posts: 1
There are two ways to do one to one mapping in the Hibernate Using the annotation
1. With Intermediate table.
2. Without Intermediate table.

For detail information see the below link
Quote:
http://javaworldwide.blogspot.in/2012/05/one-to-many-relationship-in-hibernate.html


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.