1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.ActiveRecord
20 /// Define how broken relations should be handled.
23 public enum NotFoundBehaviour
26 /// Throw an exception when the relation is broken.
31 /// Throw an exception when the relation is broken.
33 /// <remarks>this is the default behaviour</remarks>
37 /// Ignore the broken relation and update
38 /// the FK to null on the next save.
44 /// Maps a one to one association.
48 /// public class Post : ActiveRecordBase
52 /// [BelongsTo("blogid")]
55 /// get { return _blog; }
56 /// set { _blog = value; }
61 /// Please note that the 'blogid' foreign key lies on the 'Post' table.
63 [AttributeUsage(AttributeTargets
.Property
, AllowMultiple
=false), Serializable
]
64 public class BelongsToAttribute
: WithAccessAttribute
67 private String column
;
68 private String
[] compositeKeyColumns
;
69 private bool update
= true;
70 private bool insert
= true;
73 private FetchEnum fetchMethod
= FetchEnum
.Unspecified
;
74 private CascadeEnum cascade
= CascadeEnum
.None
;
75 private NotFoundBehaviour notFoundBehaviour
= NotFoundBehaviour
.Default
;
78 /// Initializes a new instance of the <see cref="BelongsToAttribute"/> class.
80 public BelongsToAttribute()
85 /// Indicates the name of the column to be used on the association.
86 /// Usually the name of the foreign key field on the underlying database.
88 public BelongsToAttribute(String column
)
94 /// Defines the target type of the association. It's usually inferred from the property type.
103 /// Defines the column used by association (usually a foreign key)
107 get { return column; }
108 set { column = value; }
112 /// Defines the Composite Key columns used by association (aka Natural Keys).
114 public String
[] CompositeKeyColumns
116 get { return compositeKeyColumns; }
117 set { compositeKeyColumns = value; }
121 /// Defines the cascading behavior of this association.
123 public CascadeEnum Cascade
125 get { return cascade; }
126 set { cascade = value; }
130 /// Defines the outer join behavior of this association.
131 /// NHibernate has deprecated the outer-join attribute so this property is
132 /// marked obsolete - it now converts to and from the fetch value.
134 [Obsolete("Use the Fetch property instead")]
135 public OuterJoinEnum OuterJoin
139 OuterJoinEnum returnValue
= OuterJoinEnum
.Auto
;
143 case FetchEnum
.Unspecified
:
144 returnValue
= OuterJoinEnum
.Auto
;
147 returnValue
= OuterJoinEnum
.True
;
149 case FetchEnum
.Select
:
150 returnValue
= OuterJoinEnum
.False
;
160 case OuterJoinEnum
.Auto
:
161 fetchMethod
= FetchEnum
.Unspecified
;
163 case OuterJoinEnum
.True
:
164 fetchMethod
= FetchEnum
.Join
;
166 case OuterJoinEnum
.False
:
167 fetchMethod
= FetchEnum
.Select
;
174 /// Chooses between outer-join fetching
175 /// or sequential select fetching.
177 public FetchEnum Fetch
179 get { return fetchMethod; }
180 set { fetchMethod = value; }
184 /// Set to <c>false</c> to ignore this association when updating entities of this ActiveRecord class.
188 get { return update; }
189 set { update = value; }
193 /// Set to <c>false</c> to ignore this association when inserting entities of this ActiveRecord class.
197 get { return insert; }
198 set { insert = value; }
202 /// Indicates whether this association allows nulls or not.
206 get { return notnull; }
207 set { notnull = value; }
211 /// Indicates whether this association is unique.
215 get { return unique; }
216 set { unique = value; }
220 /// Gets or sets the way broken relations are handled.
222 /// <value>The behaviour.</value>
223 public NotFoundBehaviour NotFoundBehaviour
225 get { return notFoundBehaviour; }
226 set { notFoundBehaviour = value; }