我有两个名为Stock和stock_daily_record的表。请找到下面的表格结构
库存
Id(主键)Int
名称varchar
stock_daily_record
Stockid(股票ID上的主键和外键)
Stock_price(主键)
我的实体类
@Entity @Table(name = "stock")
class Stock
{
@id @column(name=”id”)
Private int id;
@column(name=”name”)
Private String name;
@oneToMany(fetch = FetchType.LAZY)
Private Set<DailyStockRecord> dailyRecords;
//Getters and setters,equals
}
我的下一课DailyStockRecord仅包含复合键。如何定义这两个类之间的映射。另外如何定义DailyStockRecord实体?
注意: 请不要考虑我的数据库设计因为我试图通过这个虚拟设计来预测我的实际问题
答案 0 :(得分:0)
尝试此配置
@Entity
@Table("stock_daily_record")
public class DailyStockRecord
@EmbeddedId
private DailyStockId stockId;
@MapsId("stockId")
@ManyToOne
private Stock stock;
可嵌入的密钥如下:
@Embeddedable
public class DailyStockId
private int dailyStockId;
private int stockId;
并将您的代码更新为
@Entity
@Table(name = "stock")
public class Stock
@id @column(name=”id”)
Private int id;
@column(name=”name”)
Private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy="stock" )
Private Set<DailyStockRecord> dailyRecords;
//Getters and setters,equals
}
答案 1 :(得分:0)
@Entity @Table(name = "stock_daily_record")
class StockDailyRecord
{
@id @column(name=”Stockid”)
Private int stockId;
@column(name=”Stock_price”)
Private String stockPrice;
@ManyToOne(fetch = FetchType.LAZY)
@joinColumn(name="id")
Private Stock stock;
//Getters and setters,equals
}