KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > hibernate > HibernateProperty


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.hibernate;
30
31 import com.caucho.bytecode.JMethod;
32
33 import com.caucho.util.L10N;
34
35 import com.caucho.config.ConfigException;
36
37 import com.caucho.amber.type.EntityType;
38
39 import com.caucho.amber.field.AbstractField;
40 import com.caucho.amber.field.PropertyField;
41
42 import com.caucho.amber.table.Column;
43
44 /**
45  * configuration for an entity
46  */

47 public class HibernateProperty extends HibernateField {
48   private static final L10N L = new L10N(HibernateProperty.class);
49
50   private PropertyField _field;
51   private HibernateColumn _column = new HibernateColumn();
52   
53   HibernateProperty(EntityType type)
54   {
55     super(type);
56
57     _field = new PropertyField(type);
58
59     setField(_field);
60   }
61
62   /**
63    * Sets the name of the property.
64    */

65   public void setName(String name)
66     throws ConfigException
67   {
68     super.setName(name);
69
70     _column.setName(name);
71
72     JMethod getter = _field.getGetterMethod();
73
74     if (getter != null && getter.getReturnType().isPrimitive())
75       _column.setNotNull(true);
76   }
77
78   /**
79    * Sets the field
80    */

81   protected void setField(AbstractField field)
82   {
83     super.setField(field);
84
85     _field = (PropertyField) field;
86   }
87
88   public HibernateColumn createColumn()
89   {
90     return _column;
91   }
92
93   /**
94    * Sets the not-null column property.
95    */

96   public void setNotNull(boolean isNotNull)
97   {
98     _column.setNotNull(isNotNull);
99   }
100
101   /**
102    * Sets the length column property.
103    */

104   public void setLength(int length)
105   {
106     _column.setLength(length);
107   }
108
109   /**
110    * Sets the sql-type column property.
111    */

112   public void setSQLType(String sqlType)
113   {
114     _column.setSQLType(sqlType);
115   }
116
117   /**
118    * Sets the unique column property.
119    */

120   public void setUnique(boolean isUnique)
121   {
122     _column.setUnique(isUnique);
123   }
124
125   /**
126    * Sets the unique-key column property.
127    */

128   public void setUniqueKey(String uniqueKey)
129   {
130     _column.setUniqueKey(uniqueKey);
131   }
132
133   /**
134    * Sets the index column property.
135    */

136   public void setIndex(String index)
137   {
138     _column.setIndex(index);
139   }
140
141   /**
142    * Set true if the property should be saved on an insert.
143    */

144   public void setInsert(boolean isInsert)
145   {
146     _field.setInsert(isInsert);
147   }
148
149   /**
150    * Set true if the property should be saved on an update.
151    */

152   public void setUpdate(boolean isUpdate)
153   {
154     _field.setUpdate(isUpdate);
155   }
156
157   public void init()
158     throws ConfigException
159   {
160     super.init();
161
162     _field.setType(getType());
163     
164     Column column =
165       getOwnerType().getTable().createColumn(_column.getName(), getType());
166     column.setNotNull(_column.getNotNull());
167     column.setUnique(_column.getUnique());
168     column.setLength(_column.getLength());
169     column.setSQLType(_column.getSQLType());
170
171     _field.setColumn(column);
172
173     getOwnerType().addField(_field);
174   }
175 }
176
Popular Tags