java - Hibernate one to one mapping. Schema generation issue -


i generating schema hibernate mapping. reason 1 one mapping not getting generated properly. here classes:

@entity @table(name = "resturant") public class restaurant {      private integer restid;     private string restaurantname;     private foursquare foursquare;      @id     @column(name = "restid")     @generatedvalue(strategy = generationtype.auto)     public integer getid() {         return restid;     }      public void setid(integer id) {         this.restid = id;     }      @onetoone(fetch = fetchtype.lazy, mappedby = "restaurant", cascade = cascadetype.all)     public foursquare getfoursquare() {         return foursquare;     }      public void setfoursquare(foursquare foursquare) {         this.foursquare = foursquare;     }      @column(name = "restname")     public string getrestaurantname() {         return restaurantname;     }      public void setrestaurantname(string restaurantname) {         this.restaurantname = restaurantname;     }  } 

and,

@entity @table(name = "foursquare") public class foursquare {      private integer foursquareid;     private restaurant restaurant;      @id     @column(name = "fsid")     @generatedvalue(strategy = generationtype.auto)     public integer getfoursquareid() {         return foursquareid;     }      public void setfoursquareid(integer foursquareid) {         this.foursquareid = foursquareid;     }      @onetoone(fetch = fetchtype.lazy)     @primarykeyjoincolumn     public restaurant getrestaurant() {         return restaurant;     }      public void setrestaurant(restaurant restaurant) {         this.restaurant = restaurant;     } } 

my hbm file looks like:

<!doctype hibernate-configuration public     "-//hibernate/hibernate configuration dtd 3.0//en"     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  <hibernate-configuration>     <session-factory>          <!-- need complete configuration here. sample,              should use connection pool -->         <property name="connection.url">jdbc:mysql://localhost:3306/menus3</property>         <property name="connection.username">root</property>         <property name="connection.password"></property>         <property name="connection.driver_class">com.mysql.jdbc.driver</property>         <property name="hibernate.jdbc.batch_size">50</property>         <property name="hibernate.cache.use_second_level_cache">false</property>          <property name="dialect">org.hibernate.dialect.mysqldialect</property>          <property name="show_sql">true</property>         <property name="hibernate.hbm2ddl.auto">create</property>          <mapping class="restaurant" />         <mapping class="foursquare" />     </session-factory> </hibernate-configuration> 

here hibernateutil class:

import org.hibernate.sessionfactory; import org.hibernate.cfg.annotationconfiguration;  public final class hibernateutil {      private static sessionfactory sessionfactory;      static {         try {             sessionfactory = new annotationconfiguration().configure()                     .buildsessionfactory();         } catch (throwable ex) {              throw new exceptionininitializererror(ex);         }     }      private hibernateutil() {      }      public static sessionfactory getsessionfactory() {         return sessionfactory;     }  } 

i running simple class generate schema loading configuration:

public class test {      public static void main(string[] args) {          sessionfactory sessionfactory = hibernateutil.getsessionfactory();         session session = sessionfactory.opensession();         session.close();     } } 

this should create foreign key of restid in foursquare table not. sql looks like:

hibernate: drop table if exists foursquare hibernate: drop table if exists resturant hibernate: create table foursquare (fsid integer not null auto_increment, idinfoursquare varchar(255), primary key (fsid)) hibernate: create table resturant (restid integer not null auto_increment, restname varchar(255), staddress varchar(255), primary key (restid)) 

can point out why 1 one mapping not getting reflected in db? why foreign key column not getting generated?

you used @primarykeyjoincolumn. @primarykeyjoincolumn annotation primary key of entity used foreign key value associated entity. foreign key not getting generated. generate foreign key should remove @primarykeyjoincolumn annotaion.

@onetoone(fetch = fetchtype.lazy) //@primarykeyjoincolumn     remove annotation. public restaurant getrestaurant() {     return restaurant; } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -