forked from jhipster/generator-jhipster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_Entity.java
292 lines (272 loc) · 13.3 KB
/
_Entity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package <%=packageName%>.domain;<%
var importApiModelProperty = false;
var importJsonignore = false;
var importSet = false;
var uniqueEnums = {}; %><%- include imports -%>
<% if (databaseType == 'cassandra') { %>
import com.datastax.driver.mapping.annotations.*;<% } %><% if (importJsonignore == true) { %>
import com.fasterxml.jackson.annotation.JsonIgnore;<% } %><% if (typeof javadoc != 'undefined') { %>
import io.swagger.annotations.ApiModel;<% } %><% if (importApiModelProperty == true) { %>
import io.swagger.annotations.ApiModelProperty;<% } %><% if (hibernateCache != 'no') { %>
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;<% } %><% if (databaseType == 'mongodb') { %>
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;<% } %><% if (searchEngine == 'elasticsearch') { %>
import org.springframework.data.elasticsearch.annotations.Document;<% } %>
<% if (databaseType == 'sql') { %>
import javax.persistence.*;<% } %><% if (validation) { %>
import javax.validation.constraints.*;<% } %>
import java.io.Serializable;<% if (fieldsContainBigDecimal == true) { %>
import java.math.BigDecimal;<% } %><% if (fieldsContainBlob && databaseType === 'cassandra') { %>
import java.nio.ByteBuffer;<% } %><% if (fieldsContainLocalDate == true) { %>
import java.time.LocalDate;<% } %><% if (fieldsContainZonedDateTime == true) { %>
import java.time.ZonedDateTime;<% } %><% if (importSet == true) { %>
import java.util.HashSet;
import java.util.Set;<% } %>
import java.util.Objects;<% if (databaseType == 'cassandra') { %>
import java.util.UUID;<% } %><% Object.keys(uniqueEnums).forEach(function(element) { %>
import <%=packageName%>.domain.enumeration.<%= element %>;<% }); %>
<% if (typeof javadoc == 'undefined') { -%>
/**
* A <%= entityClass %>.
*/
<% } else { -%>
<%- formatAsClassJavadoc(javadoc) %>
@ApiModel(description = "<%- formatAsApiModel(javadoc) %>")
<% } -%>
<% if (databaseType == 'sql') { -%>
@Entity
@Table(name = "<%= entityTableName %>")<% if (hibernateCache != 'no') { %>
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)<% } %><% } %><% if (databaseType == 'mongodb') { %>
@Document(collection = "<%= entityTableName %>")<% } %><% if (databaseType == 'cassandra') { %>
@Table(name = "<%= entityInstance %>")<% } %><% if (searchEngine == 'elasticsearch') { %>
@Document(indexName = "<%= entityInstance.toLowerCase() %>")<% } %>
public class <%= entityClass %> implements Serializable {
private static final long serialVersionUID = 1L;
<% if (databaseType == 'sql') { %>
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;<% } %><% if (databaseType == 'mongodb') { %>
@Id
private String id;<% } %><% if (databaseType == 'cassandra') { %>
@PartitionKey
private UUID id;<% } %>
<%_ for (idx in fields) {
if (typeof fields[idx].javadoc != 'undefined') { _%>
<%- formatAsFieldJavadoc(fields[idx].javadoc) %>
@ApiModelProperty(value = "<%- formatAsApiModelProperty(fields[idx].javadoc) %>")
<%_ }
var required = false;
var fieldValidate = fields[idx].fieldValidate;
var fieldValidateRules = fields[idx].fieldValidateRules;
var fieldValidateRulesMaxlength = fields[idx].fieldValidateRulesMaxlength;
var fieldType = fields[idx].fieldType;
var fieldTypeBlobContent = fields[idx].fieldTypeBlobContent;
var fieldName = fields[idx].fieldName;
var fieldNameUnderscored = fields[idx].fieldNameUnderscored;
if (fieldValidate == true) {
if (fieldValidate == true && fieldValidateRules.indexOf('required') != -1) {
required = true;
} _%>
<%- include ../common/field_validators -%>
<%_ } _%>
<%_ if (databaseType == 'sql') {
if (fields[idx].fieldIsEnum) { _%>
@Enumerated(EnumType.STRING)
<%_ }
if (fieldType == 'byte[]') { _%>
@Lob
<%_ }
if (fieldType == 'LocalDate' || fieldType == 'ZonedDateTime') { _%>
@Column(name = "<%=fieldNameUnderscored %>"<% if (required) { %>, nullable = false<% } %>)
<%_ } else if (fieldType == 'BigDecimal') { _%>
@Column(name = "<%=fieldNameUnderscored %>", precision=10, scale=2<% if (required) { %>, nullable = false<% } %>)
<%_ } else { _%>
@Column(name = "<%=fieldNameUnderscored %>"<% if (fieldValidate == true) { %><% if (fieldValidateRules.indexOf('maxlength') != -1) { %>, length = <%= fieldValidateRulesMaxlength %><% } %><% if (required) { %>, nullable = false<% } %><% } %>)
<%_ }
} _%>
<%_ if (databaseType == 'mongodb') { _%>
@Field("<%=fieldNameUnderscored %>")
<%_ } _%>
<%_ if (fieldTypeBlobContent != 'text') { _%>
private <%= fieldType %> <%= fieldName %>;
<%_ } else { _%>
private String <%= fieldName %>;
<%_ } _%>
<%_ if ((fieldType === 'byte[]' || fieldType === 'ByteBuffer') && fieldTypeBlobContent != 'text') { _%>
<%_ if (databaseType == 'sql' || databaseType === 'cassandra') { _%>
@Column(name = "<%=fieldNameUnderscored %>_content_type"<% if (required && databaseType !== 'cassandra') { %>, nullable = false<% } %>)
<%_ if (required && databaseType === 'cassandra') { _%>
@NotNull
<%_ } _%>
<%_ } _%>
<%_ if (databaseType == 'mongodb') { _%>
@Field("<%=fieldNameUnderscored %>_content_type")
<%_ } _%>
private String <%= fieldName %>ContentType;
<%_ }
}
for (idx in relationships) {
var otherEntityRelationshipName = relationships[idx].otherEntityRelationshipName,
otherEntityRelationshipNamePlural = relationships[idx].otherEntityRelationshipNamePlural,
relationshipName = relationships[idx].relationshipName,
relationshipFieldName = relationships[idx].relationshipFieldName,
relationshipFieldNamePlural = relationships[idx].relationshipFieldNamePlural,
joinTableName = entityTableName + '_'+ getTableName(relationshipName),
relationshipType = relationships[idx].relationshipType,
relationshipValidate = relationships[idx].relationshipValidate,
otherEntityNameCapitalized = relationships[idx].otherEntityNameCapitalized,
ownerSide = relationships[idx].ownerSide;
if(prodDatabaseType === 'oracle' && joinTableName.length > 30) {
joinTableName = getTableName(name.substring(0, 5)) + '_' + getTableName(relationshipName.substring(0, 5)) + '_MAPPING';
}
if(prodDatabaseType === 'mysql' && joinTableName.length > 64) {
joinTableName = getTableName(name.substring(0, 10)) + '_' + getTableName(relationshipName.substring(0, 10)) + '_MAPPING';
}
if (otherEntityRelationshipName != null) {
mappedBy = otherEntityRelationshipName.charAt(0).toLowerCase() + otherEntityRelationshipName.slice(1)
}
if (typeof relationships[idx].javadoc != 'undefined') { _%>
<%- formatAsFieldJavadoc(relationships[idx].javadoc) %>
@ApiModelProperty(value = "<%- formatAsApiModelProperty(relationships[idx].javadoc) %>")
<%_ }
if (relationshipType == 'one-to-many') {
_%>
@OneToMany(mappedBy = "<%= otherEntityRelationshipName %>")
@JsonIgnore
<%_ if (hibernateCache != 'no') { _%>
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
<%_ } _%>
private Set<<%= otherEntityNameCapitalized %>> <%= relationshipFieldNamePlural %> = new HashSet<>();
<%_ } else if (relationshipType == 'many-to-one') { _%>
@ManyToOne
<%_ if (relationshipValidate) { _%>
<%- include relationship_validators -%>
<%_ }_%>
private <%= otherEntityNameCapitalized %> <%= relationshipFieldName %>;
<%_ } else if (relationshipType == 'many-to-many') { _%>
@ManyToMany<% if (ownerSide == false) { %>(mappedBy = "<%= otherEntityRelationshipNamePlural %>")
@JsonIgnore
<%_ } else { _%>
<%_ }
if (hibernateCache != 'no') { _%>
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
<%_ }
if (ownerSide == true) { _%>
<%_ if (relationshipValidate) { _%>
<%- include relationship_validators -%>
<%_ }_%>
@JoinTable(name = "<%= joinTableName %>",
joinColumns = @JoinColumn(name="<%= getPluralColumnName(name) %>_id", referencedColumnName="ID"),
inverseJoinColumns = @JoinColumn(name="<%= getPluralColumnName(relationships[idx].relationshipName) %>_id", referencedColumnName="ID"))
<%_ } _%>
private Set<<%= otherEntityNameCapitalized %>> <%= relationshipFieldNamePlural %> = new HashSet<>();
<%_ } else { _%>
<%_ if (ownerSide) { _%>
@OneToOne
<%_ if (relationshipValidate) { _%>
<%- include relationship_validators -%>
<%_ }_%>
@JoinColumn(unique = true)
<%_ } else { _%>
@OneToOne(mappedBy = "<%= otherEntityRelationshipName %>")
@JsonIgnore
<%_ } _%>
private <%= otherEntityNameCapitalized %> <%= relationshipFieldName %>;
<%_ } } _%>
public <% if (databaseType == 'sql') { %>Long<% } %><% if (databaseType == 'mongodb') { %>String<% } %><% if (databaseType == 'cassandra') { %>UUID<% } %> getId() {
return id;
}
public void setId(<% if (databaseType == 'sql') { %>Long<% } %><% if (databaseType == 'mongodb') { %>String<% } %><% if (databaseType == 'cassandra') { %>UUID<% } %> id) {
this.id = id;
}
<% for (idx in fields) {
var fieldType = fields[idx].fieldType;
var fieldTypeBlobContent = fields[idx].fieldTypeBlobContent;
var fieldName = fields[idx].fieldName;
var fieldInJavaBeanMethod = fields[idx].fieldInJavaBeanMethod; %>
<%_ if (fieldTypeBlobContent != 'text') { _%>
<%_ if (fieldType.toLowerCase() == 'boolean') { _%>
public <%= fieldType %> is<%= fieldInJavaBeanMethod %>() {
<%_ } else { _%>
public <%= fieldType %> get<%= fieldInJavaBeanMethod %>() {
<%_ } _%>
<%_ } else { _%>
public String get<%= fieldInJavaBeanMethod %>() {
<%_ } _%>
return <%= fieldName %>;
}
<%_ if (fieldTypeBlobContent != 'text') { _%>
public void set<%= fieldInJavaBeanMethod %>(<%= fieldType %> <%= fieldName %>) {
<%_ } else { _%>
public void set<%= fieldInJavaBeanMethod %>(String <%= fieldName %>) {
<%_ } _%>
this.<%= fieldName %> = <%= fieldName %>;
}
<%_ if ((fieldType == 'byte[]' || fieldType === 'ByteBuffer') && fieldTypeBlobContent != 'text') { _%>
public String get<%= fieldInJavaBeanMethod %>ContentType() {
return <%= fieldName %>ContentType;
}
public void set<%= fieldInJavaBeanMethod %>ContentType(String <%= fieldName %>ContentType) {
this.<%= fieldName %>ContentType = <%= fieldName %>ContentType;
}
<%_ } _%>
<% } %><%
for (idx in relationships) {
var relationshipFieldName = relationships[idx].relationshipFieldName,
relationshipFieldNamePlural = relationships[idx].relationshipFieldNamePlural,
relationshipType = relationships[idx].relationshipType,
otherEntityNameCapitalized = relationships[idx].otherEntityNameCapitalized,
relationshipNameCapitalized = relationships[idx].relationshipNameCapitalized,
relationshipNameCapitalizedPlural = relationships[idx].relationshipNameCapitalizedPlural,
otherEntityName = relationships[idx].otherEntityName,
otherEntityNamePlural = relationships[idx].otherEntityNamePlural;
%><% if (relationshipType == 'one-to-many' || relationshipType == 'many-to-many') { %>
public Set<<%= otherEntityNameCapitalized %>> get<%= relationshipNameCapitalizedPlural %>() {
return <%= relationshipFieldNamePlural %>;
}
public void set<%= relationshipNameCapitalizedPlural %>(Set<<%= otherEntityNameCapitalized %>> <%= otherEntityNamePlural %>) {
this.<%= relationshipFieldNamePlural %> = <%= otherEntityNamePlural %>;
}<% } else { %>
public <%= otherEntityNameCapitalized %> get<%= relationshipNameCapitalized %>() {
return <%= relationshipFieldName %>;
}
public void set<%= relationshipNameCapitalized %>(<%= otherEntityNameCapitalized %> <%= otherEntityName %>) {
this.<%= relationshipFieldName %> = <%= otherEntityName %>;
}<% } %>
<% } %>
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
<%= entityClass %> <%= entityInstance %> = (<%= entityClass %>) o;
if(<%= entityInstance %>.id == null || id == null) {
return false;
}
return Objects.equals(id, <%= entityInstance %>.id);
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
@Override
public String toString() {
return "<%= entityClass %>{" +
"id=" + id +
<%_ for (idx in fields) {
var fieldType = fields[idx].fieldType;
var fieldTypeBlobContent = fields[idx].fieldTypeBlobContent;
var fieldName = fields[idx].fieldName; _%>
", <%= fieldName %>='" + <%= fieldName %> + "'" +
<%_ if ((fieldType == 'byte[]' || fieldType === 'ByteBuffer') && fieldTypeBlobContent != 'text') { _%>
", <%= fieldName %>ContentType='" + <%= fieldName %>ContentType + "'" +
<%_ } _%>
<%_ } _%>
'}';
}
}