如何从另一个类对象

时间:2018-05-17 10:24:22

标签: java hibernate oop

我有两个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类具有私有变量long id,String name和List reviews(也从ProductReviews类获取评论)。产品类与ProductReviews具有一对多关联,反之亦然。

所以我的问题是:在上面的例子中创建第三个变量的正确语法是什么?该变量应该是Product-object的一个实例。

1 个答案:

答案 0 :(得分:0)

您可以使用以下类和映射

Pattern p = Pattern.compile("\\d");
Matcher m = p.matcher("12345");
Set<String> result = StreamSupport.stream(new MyIterator(m), false)
            .collect(Collectors.toSet());

和ProductReview类

    $this->belongsTo('ConsumablesMaster', [
            'foreignKey' => 'product_id',
            'className' => 'ConsumablesMaster'
    ])->setConditions(['SupplierInquiry.inquiry_type'=>'Consumable']);

    $this->belongsTo('ProductsMaster', [
            'foreignKey' => 'product_id',
            'className' => 'ProductsMaster'
    ])->setConditions(['SupplierInquiry.inquiry_type'=>'Chemical Product']);

  public function buildRules(RulesChecker $rules){
    $rules->add($rules->existsIn(['supplier_inquiry_id'], 'SupplierInquiry'));
    $rules->add($rules->existsIn(['product_id'], 'ConsumablesMaster'));
    $rules->add($rules->existsIn(['product_id'], 'ProductsMaster'));
    return $rules;
  }

Product Table Image

ProductReview Table Image