简体   繁体   English

Java DAO实现setObjects

[英]Java DAO implementation setObjects

There is such problem. 有这样的问题。 在此处输入图片说明

I have some entity classes Appartment , Landlord etc. Now I'm creating ApartmentDaoImpl , that is DAO implementation for Apartment . 我有一些实体类AppartmentLandlord等。现在我创建ApartmentDaoImpl ,这是Apartment DAO实现。 Everything is clear with primitive types, I can get landlordId . 原始类型一切都清楚了,我可以得到landlordId

The question is - how to set Landlord field by his id ? 问题是-如何通过其id设置Landlord字段?

This doesn't work because getId() method is non-static: 这不起作用,因为getId()方法是非静态的:

Apartment apartment = new Apartment(id);
            apartment.setRooms(rooms);
            apartment.setDescription(description);
            apartment.setFree(free);
            apartment.setPrice(price);
            apartment.setLastUpdatedPrice(lastUpdatedPrice);
            apartment.setLandlord(Landlord.getId());

First, you have to decalre Landlord Object and set its properties, eg: 首先,您必须去除地主对象并设置其属性,例如:

Landlord landlord = new Landlord();
landlord.setName("foo");
landlord.setEmail("foo@bar");
etc.

Alternatively, you can get it from DB, if the record already exists: 或者,如果记录已存在,则可以从数据库获取它:

Landlord landlord = dataAccess.getLandlordByName("hereGoesTheName");

Of course, in the latter case, you need to implement a proper DataAccess class and access its method by dataAccess Object. 当然,在后一种情况下,您需要实现适当的DataAccess类并通过dataAccess Object访问其方法。

Only then you can assign it to the Apartment Entity: 只有这样,您才能将其分配给单元实体:

apartment.setLandlord(landlord);

Aside from that, it seems to me that you don't really grasp basic concepts of OOP. 除此之外,在我看来,您并没有真正掌握OOP的基本概念。 Nothing's wrong with that, but the problem you are trying solve is quite an advanced one. 没问题,但是您要解决的问题是相当高级的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM