1
다음과 같이 nhibernate로 작업하는 2 db 테이블과 클래스 구조가 있습니다.데이터를 여러 테이블에 저장
DB
C#을 POCO 클래스는 다음과 같습니다.
고객 클래스
public class Customer
{
public virtual int CustomerID { get; set; }
public virtual string CustomerName { get; set; }
public virtual string Title { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Mobile { get; set; }
public virtual string Phone { get; set; }
public virtual string Email { get; set; }
public virtual Address BillingAddress { get; set; }
public virtual Address ShippingAddress { get; set; }
}
주소 클래스
public class Address
{
public virtual int AddressID { get; set; }
public virtual string AddressLine { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual int ZIP { get; set; }
public virtual string Fax { get; set; }
public virtual string Country { get; set; }
}
매핑 파일
customer.hbm.xml
,363,210<class name="Customer" table="Customers">
<id name="CustomerID">
<generator class="native" />
</id>
<property name="CustomerName" length="100" />
<property name="Title" length="10" />
<property name="FirstName" length="60" />
<property name="LastName" length="60" />
<property name="Mobile" length="15" />
<property name="Phone" length="15" />
<property name="Email" length="100" />
<many-to-one name="BillingAddress" class="Address" />
<many-to-one name="ShippingAddress" class="Address" />
</class>
address.hbm.xml는
<class name="Address" table="Addresses">
<id name="AddressID">
<generator class="native" />
</id>
<property name="AddressLine" length="255" />
<property name="City" length="30" />
<property name="State" length="30" />
<property name="ZIP" />
<property name="Fax" length="15" />
<property name="country" length="50" />
</class>
나는 NHibernate에 세션을 사용하여 데이터베이스 테이블을 만드는 오전. 테이블이 잘 만들고 있습니다. 위의 구조로 두 주소 유형이 지정된 고객을 저장하려면 고객 1 테이블과 주소 2 테이블을 저장하려면 어떻게해야합니까? 다음 예제 코드를 참조하십시오.
Address b_address = new Address();
Address s_address = new Address();
Customer customer = new Customer();
b_address.addressLine = "No.23, New Road";
b_address.City = "Newyork";
// more data
s_address.addressLine = "No.54, Old Road";
// more data
customer.CustomerName = "David";
// more customer data
customer.BillingAddress = b_address;
customer.ShippingAddress = s_address;
//saving the session.
session.save(customer)
위 코드는 고객 테이블에만 데이터를 삽입합니다. 이 삽입 데이터를 customers 테이블과 address 테이블 모두에 어떻게 만들 수 있습니까?
환상적입니다. Btw, 우리가 사용할 수있는 다른 옵션은 무엇입니까? – BlueBird
다음은 http://nhforge.org/doc/nh/en/index.html#mapping-declaration-manytoone 문서입니다. extract :'cascade = "all | none | save-update | delete"'. ** ** 고객의 주소가 모두 삭제되어야하므로 모든 **이 아마도 귀하의 경우에 더 적합 할 것입니다. –
나는 다른 테이블에서 모두 저장, 업데이트 또는 삭제가 작동하지 않는 또 다른 사례가 있습니다. Table1에는 충분한 데이터가 있습니다. Table2는 이것에 대해 많은 관계를 가지고 있습니다. 난 그냥 Table1에서 기본 키를 유지하지만 Table1에 새 데이터를 추가해서는 안됩니다. 어느 것을 사용할 수 있습니까? (Addresses -> [many-to-one] -> Countries) country 테이블은 이미 국가가 채워져 있습니다. 주소 테이블은 국가의 기본 키를 포함해야합니다. 데이터를로드 할 때 국가 POCO를 사용하여 전체 국가 개체를로드해야합니다. – BlueBird