Let's say I have one class "User", and it "has" a property of type "Profile". How can I configure my mappings to generate the schema and create both tables in the database?
假设我有一个类“User”,它“具有”类型为“Profile”的属性。如何配置映射以生成模式并在数据库中创建两个表?
3 个解决方案
#1
<many-to-one/>
#2
As an aside, if you are not a great fan of scripting up hibernate mappings (which I'm not) then you have a couple of other options.
顺便说一句,如果你不是编写hibernate映射脚本的忠实粉丝(我不是),那么你还有其他一些选择。
Castle ActiveRecord is one alternative - it's a layer on top of NHibernate which (among other things) lets you declare your relationships using attributes on your classes and properties.
Castle ActiveRecord是一种替代方案 - 它是NHibernate之上的一个层,它(除其他外)允许您使用类和属性上的属性声明您的关系。
And Fluent NHibernate is another - it lets you programmatically setup your classes and relationships.
而Fluent NHibernate则是另一种 - 它允许您以编程方式设置类和关系。
Both are a great improvement over writing your mapping xml by hand!
两者都比手工编写映射xml有了很大的改进!
#3
If you are asking about the mapping, you can use the join as shown below. note: you obviously have to add some additional attributes to suit your app.
如果您询问映射,可以使用如下所示的连接。注意:您显然必须添加一些其他属性以适合您的应用。
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MyNamespace" assembly="MyAssembly" default-lazy="true">
<class name="User" table="User">
<id name="Id" column="user_id" unsaved-value="0">
<generator class="native" />
</id>
<property name="Profile" column="profile" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MyNamespace" assembly="MyAssembly" default-lazy="true">
<class name="Profile" table="Profile">
<id name="Id" column="profile_id" unsaved-value="0">
<generator class="native" />
</id>
<property name="Profile" column="profile" />
</class>
</hibernate-mapping>
#1
<many-to-one/>
#2
As an aside, if you are not a great fan of scripting up hibernate mappings (which I'm not) then you have a couple of other options.
顺便说一句,如果你不是编写hibernate映射脚本的忠实粉丝(我不是),那么你还有其他一些选择。
Castle ActiveRecord is one alternative - it's a layer on top of NHibernate which (among other things) lets you declare your relationships using attributes on your classes and properties.
Castle ActiveRecord是一种替代方案 - 它是NHibernate之上的一个层,它(除其他外)允许您使用类和属性上的属性声明您的关系。
And Fluent NHibernate is another - it lets you programmatically setup your classes and relationships.
而Fluent NHibernate则是另一种 - 它允许您以编程方式设置类和关系。
Both are a great improvement over writing your mapping xml by hand!
两者都比手工编写映射xml有了很大的改进!
#3
If you are asking about the mapping, you can use the join as shown below. note: you obviously have to add some additional attributes to suit your app.
如果您询问映射,可以使用如下所示的连接。注意:您显然必须添加一些其他属性以适合您的应用。
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MyNamespace" assembly="MyAssembly" default-lazy="true">
<class name="User" table="User">
<id name="Id" column="user_id" unsaved-value="0">
<generator class="native" />
</id>
<property name="Profile" column="profile" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MyNamespace" assembly="MyAssembly" default-lazy="true">
<class name="Profile" table="Profile">
<id name="Id" column="profile_id" unsaved-value="0">
<generator class="native" />
</id>
<property name="Profile" column="profile" />
</class>
</hibernate-mapping>