从HibernateUtils创建SessionFactory对象时映射bean信息

时间:2015-06-02 11:26:18

标签: java hibernate orm mapping

我已经从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"); 

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

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

}

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

1 个答案:

答案 0 :(得分:0)

我明白了你的痛点。

请添加此行

configuration.addAnnotatedClass(Stock.class);

我认为这可以解决您的问题。