【问题标题】:mapping bean information while creating SessionFactory object from HibernateUtils从 HibernateUtils 创建 SessionFactory 对象时映射 bean 信息
【发布时间】:2015-08-16 04:02:40
【问题描述】:

我已经从 HibernateUtils 创建了一个 sessionFactory 对象,如下所示

configuration.setProperty("connection.driver_class",prop.getProperty("driverClassName"));
        configuration.setProperty("hibernate.connection.url",prop.getProperty("url"));
        configuration.setProperty("hibernate.connection.username", prop.getProperty("username"));
        configuration.setProperty("hibernate.connection.password", prop.getProperty("password"));
        configuration.setProperty("hibernate.dialect", prop.getProperty("dialect"));
        configuration.setProperty("hibernate.default_schema", prop.getProperty("schema"));
        configuration.setProperty("hibernate.hbm2ddl.auto", "validate");
        configuration.setProperty("packagesToScan", "daoBean"); 

我有一个数据库,其中已经创建了 Test 数据库和 stock 表。 现在我正在映射一个模型对象来插入信息。

@Entity
@Table(name = "stock", catalog = "mkyongdb", 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 = 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.ALL)
    public StockDetail getStockDetail() {
        return this.stockDetail;
    }

    public void setStockDetail(StockDetail stockDetail) {
        this.stockDetail = stockDetail;
    }

}

请建议将库存对象映射到插入数据。

【问题讨论】:

    标签: java hibernate orm mapping


    【解决方案1】:

    我明白了你的痛点。

    请添加此行

    configuration.addAnnotatedClass(Stock.class);
    

    我想这会解决你的问题。

    【讨论】:

      猜你喜欢
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      相关资源
      最近更新 更多