【问题标题】:hibernate OneToOne unidirectional mappinghibernate OneToOne 单向映射
【发布时间】:2017-02-25 13:06:49
【问题描述】:

我正在尝试创建由 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>

【问题讨论】:

  • 那么你的问题是什么?

标签: java hibernate one-to-one


【解决方案1】:

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

&lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;

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

或者可以使用:

禁用:&lt;property name="hbm2ddl.auto"&gt;none&lt;/property&gt;

仅更新:&lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;

查看此问题的答案以了解 hbm2ddl.auto:

Hibernate hbm2ddl.auto possible values and what they do?

希望这会有所帮助..

【讨论】:

    【解决方案2】:

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

    即: 父类: @ID @SequenceGenerator(name="sid", sequenceName="ids", initialValue=1, allocationSize=1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="sid") 私人int stid; 子类:

    【讨论】:

    • 子类不会创建任何id,它只是从表中获取id
    猜你喜欢
    • 1970-01-01
    • 2014-05-17
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多