forked from jhipster/jhipster.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreating_an_entity.html
More file actions
120 lines (104 loc) · 4.12 KB
/
Copy pathcreating_an_entity.html
File metadata and controls
120 lines (104 loc) · 4.12 KB
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
---
layout: default
title: Creating an entity
sitemap:
priority: 0.7
lastmod: 2014-10-10T00:00:00-00:00
---
<h1><i class="fa fa-bolt"></i> Creating an entity</h1>
<i><b>Please check our <a href="{{ site.url }}/video_tutorial.html">video tutorial</a> on creating a new JHipster application!</b></i>
<h2>Introduction</h2>
<p>
Once you have created your application, you will want to create <i>entities</i>. For example, you might want to create an <i>Author</i> and a <i>Book</i> entity. For each entity, you will need:
</p>
<ul>
<li>A database table</li>
<li>A Liquibase change set</li>
<li>A JPA Entity</li>
<li>A Spring Data JPA Repository</li>
<li>A Spring MVC REST Controller, which has the basic CRUD operations</li>
<li>An AngularJS router, a controller and a service</li>
<li>An HTML view</li>
</ul>
<p>
If you have several entities, you will likely want to have relationships between them. For this, you will need:
</p>
<ul>
<li>A database foreign key</li>
<li>Specific JavaScript and HTML code for managing this relationship</li>
</ul>
<p>
The "entity" sub-generator will create all the necessary files, and provide a CRUD front-end for each entity.
</p>
<p>
If you created your application with <a href="http://www.mongodb.org" target="_blank">MongoDB</a> instead of a more classical SQL/JPA solution, JHipster will still generate your entities correctly (you will have MongoDB documents instead of JPA entities), but of course you won't be able to have relationships between your entities.
</p>
<p>This is a short tutorial on creating two entities (a Author and a Book) which have a one-to-many relationship.<p>
<h2>Generate the "Author" entity</h2>
<p>As we want to have a one-to-many relationship between Authors and Books (one author can write many books), we need to create the Author first. At the database level, JHipster will then be able to add a foreign key on the Book table, linking to the Author table.</p>
<p>
<code>
yo jhipster:entity author
</code>
</p>
<p>
Answer the next questions concerning the fields of this entity, the author has:
<ul>
<li>a "name" of type "String"</li>
<li>a "birthDate" of type "LocalDate"</li>
</ul>
</p>
<p>
Then answer the questions concerning the relationships, the author has:
<ul>
<li>A one-to-many relationship with the "book" entity (which doesn't exist yet)</li>
</ul>
</p>
<h2>Generate the "Book" entity</h2>
<p>
<code>
yo jhipster:entity book
</code>
</p>
<p>
Answer the next questions concerning the fields of this entity, the book has:
<ul>
<li>a "title", of type "String"</li>
<li>a "description", of type "String"</li>
<li>a "publicationDate", of type "LocalDate"</li>
<li>a "price", of type "BigDecimal"</li>
</ul>
</p>
<p>
Then answer the questions concerning the relationships, the book:
<ul>
<li>Has many-to-one relationship with the "author" entity</li>
<li>And this relationship uses the "name" field (from the Author entity) to be displayed</li>
</ul>
</p>
<h2>Check the generated code</h2>
<p>
Run the generated test suite, with <code>mvn test</code>, which will test the Author entity and the Book entity.
</p>
<p>
Launch the application (for example with <code>mvn spring-boot:run</code>), log in and select the "Author" and "Book"
entities in the "entities" menu.
</p>
<p>
Check the database tables, to see if your data is correctly inserted.
</p>
<h2>Improve the generated code</h2>
<p>
The generated files contain all the basic CRUD operations, and don't need to be modified if your needs are simple.
</p>
<p>
If you want to modify the generated code or the database schema, you should follow our <a href="{{ site.url }}/development.html">development guide</a>
</p>
<p>
If you want some more complex business behaviors, you might need to add a Spring <code>@Service</code> class, using the <a href="{{ site.url }}/creating_a_service.html">service sub-generator</a>.
</p>
<h2>You're done!</h2>
<p>
Your generated CRUD page should look like this:
</p>
<img src="images/screenshot_5.png"/>