Seam practice (3): Entities

Source: Internet
Author: User
For an application, the object is the data basis of the application. The initial design begins with the data model design. Let's start with the seam entities.

In seam, you can create entity objects in two ways. One is generated from the database, the other is created by yourself, and the other is created by seam to help you build the corresponding data table. From the application perspective, there are no two methods
Difference, but from the perspective of development, we recommend using the generation method. This leaves a lot of time for coding. Haha. Let's take a look at the process of creating an entity first.

The premise is that your project has been created. (for how to create a project, see 1 in this series.)

Generally, entity classes are placed under src/model. It is easy to identify the Business Objects and Data Objects. (For details about the seam layer, I will not mention it here ). Right-click the src/model folder and choose "New"> "Seam entity. You can see the following interface:

You only need to enter a name here. Explain what they mean:
SEAM project: That project
Seam entity class name: The name of this class
Package name: the name of the package
Master Page name: This automatically generated guy is the name of your list page and Business Action processing (Action)
Page name: this will also be generated automatically. It refers to the name of the detail page and the name of the Business Action processing (the two are with the same name)

Just give a name and click Finish. It may take some time because seam automatically creates an XHTML page and is trying to open it. It takes some time to open this item.
After the creation is successful. There will be one more class under the package you selected. There are only three attributes: ID, version, and name. Take the user class I created as an example:
@ Entity
Public class user implements serializable {

// Seam-gen attributes (you shoshould probably edit these)
Private long ID;
Private integer version;
Private string name;

// Add additional entity attributes

// Seam-gen attribute getters/setters with annotations (you probably shocould edit)

@ ID @ generatedvalue
Public long GETID (){
Return ID;
}

Public void setid (long ID ){
This. ID = ID;
}

@ Version
Public integer getversion (){
Return version;
}

Private void setversion (integer version ){
This. Version = version;
}

@ Length (max = 20)
Public String getname (){
Return name;
}

Public void setname (string name ){
This. Name = Name;
}
}

This class implements self-serializable because this class needs to be serialized and can be used for writing in special cases. Explain these annotations first.
@ Entity: annotation is an object and a JPA tag. It indicates that it is the class name. Of course, you can also make these two things not the corresponding relationship. Add a @ table annotation under @ entity. For example
@ Entity
@ Table (name = "booktype", catalog = "test ")
@ ID: primary key ID. To ensure the uniqueness of the object, we recommend that you add a primary key for each entity. Otherwise, you will be waiting for an error. Haha
@ Generatedvalue indicates that the primary key value is automatically generated.
@ Version: This is the Optimistic Locking Mechanism of JPA. If you are interested in learning about optimistic locks and pessimistic locks, you can refer to the hibernate documents. The article is too long and will not be introduced here.
@ Length (max = 20) the maximum length is 20, which is useful for verification.
Well. This entity class is so simple. Well, you need to add other fields ..
In fact, the entire crud page and processing method have been written. In this case, you only need to deploy the project to the server and then access the list page. Forgot to mention, the page I generated here is
Userlist.xhtmland user.xhtml. The project name is seamtest. So I want to access http: // localhost: 8080
/Seamtest/userlist. seam, an add, delete, modify, and query is displayed. Is there anything faster than this. Now you need to add things on the page. CTRL + c
And Ctrl + v. If the page does not comply with the rules, you can call it again. Here we will briefly describe the Help Mechanism of seam, or take my user as an example: <H: inputtext id = "name" required = "true"
Value = "# {userhome. instance. name}"/>

The Help Mechanism of seam is based on value. That is to say, no matter what your ID is. All the content in this text box must be assigned to the name. This is the seam mechanism. In this way, there will be a problem. Later, let's take a look at it now.

Generate entities. It is easier to generate entities using database tables. Right-click the src/model folder and choose new> seam generate entities. A window like this will pop up:

You do not need to modify any options. Simply finish. In this way, the entity will be generated, and even the page will be generated. Addition, deletion, modification, and query are also completed. You just need to fine-tune it. This
Note that the table you just created through seam is also generated into your application as a normal table. In this way, if you are using a Linux platform, one more
Userlist.xhtml(userlist.xhtml ). What if I don't know what it will do on windows?
Write ).

Possible error: Some netizens told me that an error occurred when they generated the entities. The error message is that the template or session package cannot be found. This is because the version used is too new. JBoss tools3.0 only supports seam2.0. Do not use 2.1. I post the error message for your reference.

This is probably the case. You are welcome to discuss any issues.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.