[英]How to create a variable from another class object
我有两个Java类,Product和ProductReview。 ProductReview除了具有变量long ID和String之外,还具有一个名为Product的变量,该变量应包含Product类中的一个对象。 例:
@Entity
@Table(name="Product Reviews")
public class ProductReview implements java.io.Serializable {
@Id
@Column
private long id;
@Column
private String review;
private (stuck here, how do I type another classes object as a variable?)
Product类具有私有变量,长id,字符串名称和列表评论(也从ProductReviews类获得评论)。 产品类与产品评论之间存在一对多关联,反之亦然。
所以我的问题是:在上面的示例中,创建第三个变量的正确语法是什么? 该变量应该是产品对象的实例。
您可以在下面的类和映射中使用
@Entity
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="Product_Id")
private int id;
@Column(name="Product_Name")
private String name;
@Column(name="Product_Price")
private double price;
@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL,orphanRemoval=true,mappedBy="product")
private Set<ProductReview> review = new HashSet<ProductReview>();
public Product(String name, double price) {
this.name = name;
this.price = price;
}
//Getter and Setter
}
和ProductReview类
@Entity
public class ProductReview {
@javax.persistence.Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="Review_Id")
private int Id;
@Column(name="Review")
private String review;
@ManyToOne()
@JoinColumn(name="Product_Id")
private Product product;
public ProductReview(String review, Product product) {
this.review = review;
this.product = product;
}
public ProductReview() {
}
//Getter and Setter
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.