annotations - Hibernate error: one-to-one mapping on superclass and subclass -


i'm working on project hibernate , mysql, use annotation mapping database data object model. need mapping one-to-one between superclass , subclass on id primary key of classes. i've modified sample of tutorial: http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example/

ddl script:

create database if not exists mkyong; use mkyong;  -- -- definition of table `stock` --  drop table if exists `stock`; create table `stock` (   `stock_id` int(10) unsigned not null auto_increment,   `stock_code` varchar(10) not null,   `stock_name` varchar(20) not null,   primary key (`stock_id`) using btree,   unique key `uni_stock_name` (`stock_name`),   unique key `uni_stock_id` (`stock_code`) using btree ) engine=innodb auto_increment=34 default charset=utf8;  -- -- definition of table `stock_detail` -- drop table if exists `mkyong`.`stock_detail`; create table  `mkyong`.`stock_detail` (   `stock_id` int(10) unsigned not null auto_increment,   `comp_name` varchar(100) not null,   `comp_desc` varchar(255) default null,   `remark` varchar(255) default null,   `listed_date` date not null,   primary key (`stock_id`) using btree,   constraint `fk_stock_id` foreign key (`stock_id`) references `stock` (`stock_id`) ) engine=innodb auto_increment=86 default charset=utf8; 

here code after modifying: stock.java (superclass)

package com.mkyong.common;  import javax.persistence.column; import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.onetoone; import javax.persistence.table; import javax.persistence.uniqueconstraint;  import org.hibernate.annotations.cascade; import org.hibernate.annotations.cascadetype;  @entity @table(name = "stock", catalog = "mkyong", uniqueconstraints = {         @uniqueconstraint(columnnames = "stock_name"),         @uniqueconstraint(columnnames = "stock_code") }) public class stock implements java.io.serializable {      private integer stockid;     private string stockcode;     private string stockname;     private stockdetail stockdetail;      public stock() {     }      public stock(string stockcode, string stockname) {         this.stockcode = stockcode;         this.stockname = stockname;     }      public stock(string stockcode, string stockname, stockdetail stockdetail) {         this.stockcode = stockcode;         this.stockname = stockname;         this.stockdetail = stockdetail;     }      @id     @generatedvalue(strategy = generationtype.identity)     @column(name = "stock_id", unique = true, nullable = false)     public integer getstockid() {         return this.stockid;     }      public void setstockid(integer stockid) {         this.stockid = stockid;     }      @column(name = "stock_code", unique = true, nullable = false, length = 10)     public string getstockcode() {         return this.stockcode;     }      public void setstockcode(string stockcode) {         this.stockcode = stockcode;     }      @column(name = "stock_name", unique = true, nullable = false, length = 20)     public string getstockname() {         return this.stockname;     }      public void setstockname(string stockname) {         this.stockname = stockname;     }      @onetoone(fetch = fetchtype.lazy, mappedby = "stock")     @cascade({cascadetype.save_update})     public stockdetail getstockdetail() {         return this.stockdetail;     }      public void setstockdetail(stockdetail stockdetail) {         this.stockdetail = stockdetail;     }  } 

and stockdetail.java (subclass)

package com.mkyong.common;  // generated jan 19, 2010 6:46:22 pm hibernate tools 3.2.5.beta  import java.util.date; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.onetoone; import javax.persistence.primarykeyjoincolumn; import javax.persistence.table; import javax.persistence.temporal; import javax.persistence.temporaltype; import org.hibernate.annotations.genericgenerator; import org.hibernate.annotations.parameter;  /**  * stockdetail generated hbm2java  */ @entity @table(name = "stock_detail", catalog = "mkyong") public class stockdetail extends stock implements java.io.serializable {      private integer stockid;     private stock stock;     private string compname;     private string compdesc;     private string remark;     private date listeddate;      public stockdetail() {     }      public stockdetail(stock stock, string compname, date listeddate) {         this.stock = stock;         this.compname = compname;         this.listeddate = listeddate;     }      public stockdetail(stock stock, string compname, string compdesc,             string remark, date listeddate) {         this.stock = stock;         this.compname = compname;         this.compdesc = compdesc;         this.remark = remark;         this.listeddate = listeddate;     }      @genericgenerator(name = "generator", strategy = "foreign", parameters = @parameter(name = "property", value = "stock"))     @id     @generatedvalue(generator = "generator")     @column(name = "stock_id", unique = true, nullable = false)     public integer getstockid() {         return this.stockid;     }      public void setstockid(integer stockid) {         this.stockid = stockid;     }      @onetoone(fetch = fetchtype.lazy)     @primarykeyjoincolumn     public stock getstock() {         return this.stock;     }      public void setstock(stock stock) {         this.stock = stock;     }      @column(name = "comp_name", nullable = false, length = 100)     public string getcompname() {         return this.compname;     }      public void setcompname(string compname) {         this.compname = compname;     }      @column(name = "comp_desc")     public string getcompdesc() {         return this.compdesc;     }      public void setcompdesc(string compdesc) {         this.compdesc = compdesc;     }      @column(name = "remark")     public string getremark() {         return this.remark;     }      public void setremark(string remark) {         this.remark = remark;     }      @temporal(temporaltype.date)     @column(name = "listed_date", nullable = false, length = 10)     public date getlisteddate() {         return this.listeddate;     }      public void setlisteddate(date listeddate) {         this.listeddate = listeddate;     } } 

i've tried insert record: mainapplication.java

package com.mkyong.common;  import java.util.date; import org.hibernate.session; import com.mkyong.persistence.hibernateutil;  public class app {      public static void main(string[] args) {         system.out.println("maven + hibernate one-to-one example + mysql");         session session = hibernateutil.getsessionfactory().opensession();          session.begintransaction();          stock stock = new stock();          stock.setstockcode("9588");         stock.setstockname("msft");          stockdetail stockdetail = new stockdetail();         stockdetail.setcompname("microsoft company");         stockdetail.setcompdesc("blah blah");         stockdetail.setlisteddate(new date());          stock.setstockdetail(stockdetail);         stockdetail.setstock(stock);          session.save(stock);          session.gettransaction().commit();      } } 

but error occurred: initial sessionfactory creation failed.org.hibernate.annotationexception: unable define/override @id(s) on subclass: com.mkyong.common.stockdetail

could please me resolve issue? thank much.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -