hibernate OneToOne单向映射

时间:2016-05-21 03:59:23

标签: java hibernate one-to-one

我正在尝试创建由OneToOne单向映射关联的表。 我写了一个小的演示程序,表格正在创建但显示空值。控制台上没有例外。如果有人可以帮助为什么企业不会在表格中保留,那就太棒了。

由于

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 + "]";
    }



}

这是StockDailyRecords类

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 + "]";
    }


}

这是我的主要课程

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
}

}

这是hibernate.cfg.xml文件:

<?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>

2 个答案:

答案 0 :(得分:0)

注释掉这一行并运行你的应用程序。

  

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

每次运行应用程序时,它都会删除然后重新创建表。

或者可以使用:

  

要停用:<property name="hbm2ddl.auto">none</property>

     

仅更新:<property name="hbm2ddl.auto">update</property>

检查这个问题的答案,了解hbm2ddl.auto:

Hibernate hbm2ddl.auto possible values and what they do?

希望这会有所帮助..

答案 1 :(得分:0)

你必须在父类id中创建序列或表,

即: 父类:     @ID     @SequenceGenerator(name =&#34; sid&#34;,sequenceName =&#34; ids&#34;,initialValue = 1,allocationSize = 1)     @GeneratedValue(strategy = GenerationType.SEQUENCE,generator =&#34; sid&#34;)     private int stid; 儿童班: