From the course: Java EE 7: Enterprise JavaBeans (EJB)

Add an entity for interacting with DB

From the course: Java EE 7: Enterprise JavaBeans (EJB)

Start my 1-month free trial

Add an entity for interacting with DB

- [Instructor] The next step is to add an entity file. Basically the entity allows us to interact with the database, and then with the entity, all the other files are going to interact with. Let's go ahead and add that file. The first thing you're going to do is right-click on VinylApp-egb. Right-click there, select New, Other, and then go to Persistence, and then select Entity Class. Then click Next. And then what we're going to do is basically add a name for the entity. For this one what we're going to do is call it RecordsEntity, like so. And then for the package, what we're going to do is create a new package called egb. Then click Next. And then make sure that we have VinylApp-egbPU here, so we're good. Then for the actual data source, we're going to do gdbc/sample. Basically what we want to do is select the sample database that we have in our services. I'm going to click Finish and then I'll show you which one it is. Click Finish and then click on Service. This is the actual database that we're going to connect to from the entity here. The next thing we need to do now is create basically the items that we're going to have in our database, so the two strings that we're going to use to create elements for our records. What I'm going to do now is click on line 21 and return, and then I'm going to create two elements. One is private String recordName, and then private String recordArtist, like so. After we created those two elements here, we need to create the getters and the setters. Basically the getters and the setters will allow us to get information from the database. And then from the other elements in this application, we can use the methods that we're going to create from those getters and setters. Let's right-click directly into the editor and then click on Insert Code, and then click on Getter and Setter. We want to set those for the two elements that were created, and then click Generate. Now if you scroll down, that bean's actually created the getters and the setters for you. So you have the getRecordName, setRecordName, getRecordArtist, setRecordArtist, and so on, so forth. Now that we have our entity, we can actually save this and actually move on.

Contents