facebook

PK and FK with hibernate

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #225660 Reply

    Steve
    Member

    Hi,

    I was wondering if you could help me with this. Which id Generator should I use for PRODUCT_IMAGES mapping?

    CREATE TABLE PRODUCTS(
    Product_ID INT NOT NULL,
    Product_Code VARCHAR(15) NOT NULL,
    Long_Name VARCHAR(50),
    Short_Name VARCHAR(10),
    VOD_Parameter VARCHAR(50),
    Product_Count INT,
    Initial_Price DOUBLE NOT NULL,
    Minimum_Bid DOUBLE,
    End_Date DATETIME NOT NULL,
    Sell_Price DOUBLE,
    PRIMARY KEY (Product_ID)
    )TYPE=INNODB
    ;

    CREATE TABLE PRODUCT_IMAGES(
    Product_ID INT NOT NULL,
    Icon BLOB,
    Icon_Name VARCHAR(20),
    VOD_Background BLOB,
    VOD_Background_Name VARCHAR(20),
    VOD_Substitute BLOB,
    VOD_Substitute_Name VARCHAR(20),
    More_Info BLOB,
    More_Info_Name VARCHAR(20),
    PRIMARY KEY (Product_ID)
    )TYPE=INNODB
    ;

    where PRODUCT.PRODUCT_ID is PK and is used as FK in PRODUCT_IMAGES. I have tried Foriegn but i get a cannot create key error.

    Or could anyone tell me how to proceed from here? I believe you need to tell hibernate which class the PK comes from right? and the mapping should be <id-composite>? I dont know…

    Thank you

    #225689 Reply

    Riyad Kalla
    Member

    Which id Generator should I use for PRODUCT_IMAGES mapping?

    If you simply make the Product_ID field “autoincrement” then you can use the generator “native”, that is the easiest. Let the DB do what it is good at 😉

    #225731 Reply

    this falls under one-to-one relationship. the mapping should be done this way:
    <one-to-one
    name=”productImages”
    class=”productImages”
    constrained=”true”
    unique=”true”
    property-ref=”product”
    cascade=”all”
    />

    though I am not sure how to do this with Myeclipse. It would be great if we dont need to do this by hand.

    Also speaking of images, Myeclipse uses String instead of Blobs.

    😉

    #225741 Reply

    Steve
    Member

    Unfortunately MYSQL does not support native… or at least did not work with my code.
    But I was able to successfully done it using one-to-one. May I post it for other people’s reference:

    For Product Class:

    public class Products implements Serializable{
    private java.lang.Integer productId;
    private ProductImages productImages;
    private java.lang.Double sellPrice;
    //etc
    }

    <class name=”Products” table=”PRODUCTS”>
    <id name=”productId” column=”Product_ID” type=”java.lang.Integer”>
    <generator class=”increment”/>
    </id>
    <one-to-one name=”productImages” class=”ProductImages” cascade=”save-update”/>
    …all property here
    </class>

    For ProductImages Class:

    public class ProductImages {

    private Products products;
    private Integer productId;
    private String iconName;

    <class name=”ProductImages” table=”PRODUCT_IMAGES”>
    <id name=”productId” column=”Product_ID” type=”java.lang.Integer”>
    <generator class=”foreign”>
    <param name=”property”>products</param>
    </generator>
    </id>

    <one-to-one name=”products” class=”Products” constrained=”true”/>
    …all property here
    </class>

    #225973 Reply

    raorba
    Member

    @debianJack wrote:

    Hi,

    I was wondering if you could help me with this. Which id Generator should I use for PRODUCT_IMAGES mapping?

    CREATE TABLE PRODUCTS(
    Product_ID INT NOT NULL,
    Product_Code VARCHAR(15) NOT NULL,
    Long_Name VARCHAR(50),
    Short_Name VARCHAR(10),
    VOD_Parameter VARCHAR(50),
    Product_Count INT,
    Initial_Price DOUBLE NOT NULL,
    Minimum_Bid DOUBLE,
    End_Date DATETIME NOT NULL,
    Sell_Price DOUBLE,
    PRIMARY KEY (Product_ID)
    )TYPE=INNODB
    ;

    CREATE TABLE PRODUCT_IMAGES(
    Product_ID INT NOT NULL,
    Icon BLOB,
    Icon_Name VARCHAR(20),
    VOD_Background BLOB,
    VOD_Background_Name VARCHAR(20),
    VOD_Substitute BLOB,
    VOD_Substitute_Name VARCHAR(20),
    More_Info BLOB,
    More_Info_Name VARCHAR(20),
    PRIMARY KEY (Product_ID)
    )TYPE=INNODB
    ;

    where PRODUCT.PRODUCT_ID is PK and is used as FK in PRODUCT_IMAGES. I have tried Foriegn but i get a cannot create key error.

    Or could anyone tell me how to proceed from here? I believe you need to tell hibernate which class the PK comes from right? and the mapping should be <id-composite>? I dont know…

    Thank you

    Hi there (please be gentle with my english)

    I’m using these mapping files for an app with one-to-many relation. For both relations I’m using HILO generator or NATIVE generator. In MYSQL, NATIVE generator creates an AUTO_INCREMENT column.

    My entities are: UserInfo and TaskHistoryInfo. UserInfo (master relation) has many TaskHistoryInfo (slave relation)

    USERINFO.HBM.XML
    ————————————————-

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    <hibernate-mapping>
      <class name="model.UserInfo" table="USR_AUTORIZADO">
        <id name="id" column="NU_REGISTRO_USR" type="long" unsaved-value="null">
            <!--<generator class="hilo" />-->
            <!--<generator class="native"/>-->
        </id>
    
        <property name="firstName" column="NB_USUARIO_CONTR"
        type="string" length="25" not-null="true" />
    
        <property name="lastName" column="TX_APELLIDO_CONTR"
        type="string" length="25" not-null="true" />
    
        <property name="email" column="TX_EMAIL_CONTR"
        type="string" length="50" not-null="true" />
    
        <property name="telephone" column="NU_TELF_CONTR"
        type="string" length="12" not-null="true" />
    
        <property name="userName" column="CO_USUARIO_CONTR" type="string"
        length="8" not-null="true" />
    
        <property name="password" column="CO_PASSWORD_CONTR"
        type="string" length="8" not-null="true" />
    
        <set name="taskHistory" inverse="true" cascade="all" lazy="false">
          <key column="NU_REGISTRO_USR" />
          <one-to-many class="model.TaskInfoHistory" />
        </set>
    
      </class>
    </hibernate-mapping>
    
    

    TASKINFOHISTORY.HBM.XML
    ———————————————–

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    <hibernate-mapping>
    
      <class name="model.TaskInfoHistory" table="H_INFO_PAG" mutable="true">
        <id name="id" column="NU_REGISTRO_H_INFO" type="long" unsaved-value="null">
            <!--<generator class="hilo" />-->
            <!--<generator class="native"/>-->
        </id>
    
        <!-- <property name="generatedId" column="CO_UBICA_FUENT_INF"
        type="string" length="200" not-null="true" /> -->
    
        <property name="text" column="TX_PUBLICA_INF_PAG" type="string"
        length="100" not-null="true" />
    
        <property name="date" column="FE_PUBLICA_INF_PAG" type="date"
        not-null="true" />
    
        <property name="pageReference"
        column="NB_FUENT_INFO" type="string" length="30"
        not-null="true" />
        
        <property name="computerName" column="TX_PC_PUBLICA_INFO"
        type="string" length="50" not-null="true" />
        
        <many-to-one name="userInfo" class="model.UserInfo" column="NU_REGISTRO_USR" not-null="true" />
        
      </class>
    </hibernate-mapping>
    

    In my case, both generators (HILO and NATIVE) work well in my MYSQL installation. I’m not test on INNODB but I think it should works too.

    In your message you didn’t say if is a one-to-many or a one-to-one relationship, but the sample should work fine in both cases

    Regards.

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: PK and FK with hibernate

You must be logged in to post in the forum log in