2016-05-21 4 views
1

OneToOne単方向マッピングによって関連付けられたテーブルを作成しようとしています。 私は小さなデモプログラムを書いていますが、表は作成されていますが、null値を示しています。コンソールに例外はありません。hibernate OneToOne単方向マッピング

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost:3306/emprec</property> 
     <property name="connection.username">***</property> 
     <property name="connection.password">***</property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 

     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

     <!-- Disable the second-level cache --> 
     <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 

     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property> 
     <property name="hbm2ddl.auto">create</property> 

     <!-- Mention here all the model classes along with their package name --> 

     <mapping class="com.abc.unidirectional.Stock"/> 
     <mapping class="com.abc.unidirectional.StockDailyRecords"/> 

    </session-factory> 
</hibernate-configuration> 

おかげ

package com.abc.unidirectional; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name = "OneToOne_Stock_Unidirectional") 
public class Stock implements java.io.Serializable { 
    private static final long serialVersionId=1L; 

    @Id 
    @GeneratedValue 
    private int stockId; 

    private String stockCode; 
    private String stockName; 


    private StockDailyRecords stockDailyRecords; 

    public Stock() { 
    } 

    public int getStockId() { 
     return stockId; 
    } 

    public void setStockId(int stockId) { 
     this.stockId = stockId; 
    } 

    public String getStockCode() { 
     return this.stockCode; 
    } 

    public void setStockCode(String stockCode) { 
     this.stockCode = stockCode; 
    } 

    public String getStockName() { 
     return this.stockName; 
    } 

    public void setStockName(String stockName) { 
     this.stockName = stockName; 
    } 

    public StockDailyRecords getStockDailyRecords() { 
     return stockDailyRecords; 
    } 

    public void setStockDailyRecords(StockDailyRecords stockDailyRecords) { 
     this.stockDailyRecords = stockDailyRecords; 
    } 

    @Override 
    public String toString() { 
     return "Stock [stockId=" + stockId + ", stockCode=" + stockCode + ", stockName=" + stockName 
       + ", stockDailyRecords=" + stockDailyRecords + "]"; 
    } 



} 

は、これはこれはこれは、hibernate.cfg.xmlファイルである

package com.abc.unidirectional; 

import java.util.Date; 
import org.hibernate.Transaction; 

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import com.cavalier.unidirectional.HibernateUtil; 
import org.hibernate.cfg.AnnotationConfiguration; 


public class Main { 

public static void main(String[] args) { 
    Main obj = new Main(); 
    obj.saveInTable(); 
} 

public Integer saveInTable(){ 

    Stock stock = new Stock(); 

    stock.setStockCode("705"); 
    stock.setStockName("MICROSOFT"); 



    StockDailyRecords sdrecords1 = new StockDailyRecords(); 

    sdrecords1.setPriceOpen(50.30); 
    sdrecords1.setPriceClose(80); 
    sdrecords1.setPriceChange(10.9f); 
    sdrecords1.setListedDate(new Date()); 
    sdrecords1.setStock(stock); 


    stock.setStockDailyRecords(sdrecords1); 

    SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); 
    Session session= sessionFactory.openSession(); 
    Transaction transaction = null; 
    Integer stockId = null; 
    try { 
     transaction = session.beginTransaction(); 
     session.save(stock); 
     session.save(sdrecords1); 
     transaction.commit(); 

    } catch (HibernateException e) { 
     transaction.rollback(); 
     e.printStackTrace(); 

    } finally { 
     session.close(); 
     sessionFactory.close(); 
    } 
    return stockId; //returns serializable primary key 
} 

} 

私のメインクラスです

package com.abc.unidirectional; 

import java.util.Date; 
import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
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; 

@Entity 
@Table(name = "OneToOne_Unidirectional") 
public class StockDailyRecords implements java.io.Serializable { 
    private static final long serialVersionId = 1L; 

    @Id 
    @GeneratedValue(generator = "newGenerator") //name of the primary key generator 
    @GenericGenerator(name = "newGenerator", strategy = "foreign",parameters = { @Parameter(value = "stockDailyRecords", name = "property") }) 
    private int stockId; 

    private String recordId; 
    private double priceOpen; 
    private double priceClose; 
    private float priceChange; 
    private Date listedDate; 

    @OneToOne 
    @JoinColumn(name = "stockId") 
    private Stock stock; 

    public StockDailyRecords() { 
    } 

    public int getStockId() { 
     return stockId; 
    } 


    public void setStockId(int stockId) { 
     this.stockId = stockId; 
    } 

    public String getRecordId() { 
     return recordId; 
    } 

    public void setRecordId(String recordId) { 
     this.recordId = recordId; 
    } 

    public Stock getStock() { 
     return this.stock; 
    } 

    public void setStock(Stock stock) { 
     this.stock = stock; 
    } 

    public double getPriceOpen() { 
     return priceOpen; 
    } 

    public void setPriceOpen(double priceOpen) { 
     this.priceOpen = priceOpen; 
    } 

    public double getPriceClose() { 
     return priceClose; 
    } 

    public void setPriceClose(double priceClose) { 
     this.priceClose = priceClose; 
    } 

    public float getPriceChange() { 
     return priceChange; 
    } 

    public void setPriceChange(float priceChange) { 
     this.priceChange = priceChange; 
    } 

    //@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; 
    } 

    @Override 
    public String toString() { 
     return "StockDailyRecords [stockId=" + stockId + ", recordId=" + recordId + ", priceOpen=" + priceOpen 
       + ", priceClose=" + priceClose + ", priceChange=" + priceChange + ", listedDate=" + listedDate 
       + ", stock=" + stock + "]"; 
    } 


} 

StockDailyRecordsクラスです

+0

あなたの問題は何ですか? – Unknown

答えて

0

この行をコメントアウトし、アプリケーションを実行します。

<property name="hbm2ddl.auto">create</property>

あなたはそれを削除し、テーブルを再作成するアプリケーションを実行して毎回。

または多分を使用します。

無効にするには:<property name="hbm2ddl.auto">none</property>

のみ更新するには:<property name="hbm2ddl.auto">update</property>

Checkのはhbm2ddl.auto理解して答えるこの質問:

Hibernate hbm2ddl.auto possible values and what they do?

を このことができます

希望..あなたは、親クラスIDのシーケンスまたはテーブルを作成する必要が

0

すなわち: 親クラス: @Id @SequenceGenerator(名前= "SID"、sequenceName = "IDS "、initialValue = 1、allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE、generator =" sid ") private int stid; 子クラス:

+0

子クラスはIDを作成せず、テーブルからIDを取得するだけです –

関連する問題