Creating an Entity class

I’ve got a mysql database with a table that looks like:

mysql> describe sgname;
±-------±------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±-------±------------±-----±----±--------±---------------+
| sgid | int(11) | NO | PRI | NULL | auto_increment |
| sgname | varchar(64) | NO | | NULL | |
±-------±------------±-----±----±--------±---------------+
2 rows in set (0.00 sec)

I would like to create a java class that I can use in a Vaadin project to access the database table. I tried this by manually creating a file that looks like:

@Entity
public class Sgname extends AbstractPojo{
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "sgid")
private Integer sgid;
@Basic(optional = false)
@Column(name = "sgname")
private String sgname;

etc. etc.

This is basically a copy of the code that was automatically generated for me when I did this in a Netbeans environment…

But I have a few questions - some of these may be simplistic, but I have not used Eclipse before, and I am just trying to figure out how Vaadin works too… Thanks in advance for any tips or pointers. Links to documentation would be fine too…

  1. Do I need to rename the database field from ‘sgid’ to ‘id’ to make it work with the appFoundation library?

  2. Is there an Eclipse wizard that would let me build the Entity class? Can I build the class automatically (ie. have Eclipse look at the database and generate the entity class file)?

  3. I see a File->New->Entity wizard, but when I tried to invoke that it said only JPA projects could use it - my project was a Vaadin project - can I create a project that is both Vaadin and JPA enabled? If so, can I modify an existing project to add JPA capabilities? How?

  4. In my Netbeans environment, I was able to generate multiple entity classes and the relationships among them. So the file above also has this in it:

    @ManyToMany(mappedBy = “sgnameCollection”)
    private Collection usersCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = “sgid”)
    private Collection maintenanceCollection;

Can I do that in the Eclipse/Vaadin environment? If so, how?

I suspect I will have more questions in this area, but if you can get me started, I would be most grateful.

thanks,

nbc

If you take a look at the
AbstractPojo
class which your entities needs to extend, you can see that there already is a primary key field called “id”, thus, your database table needs to have a corresponding “id” field. I’m not sure if it’s allowed to have multiple columns annotated with @Id, but by looking at your example, it looks like you wouldn’t need the sgid field at all, since the superclass’s id field has the same purpose.

What comes to Eclipse, I have no experience with JPA plugins as I’ve always written my entities manually. How JPA relates to Vaadin: there are no restrictions, you should be able to use the same entities in a Vaadin application as you would in any other application.

That makes sense. Thanks. I was able to get a different test program running using the SQLContainer that reads my existing sgname database table (using my original Entity class). So that’s a start. If I move towards extending AbstractPojo, then I will get rid of the sgid field in my class (and rename the field to ‘id’ in the database).

But I could still use some information on using Eclipse to automatically generate Entity classes from a database, and on building Entity classes containing foreign key relationships - can anyone help me with that?

Much obliged,

nbc