DB Importer is an Eclipse plug-in that generates JPA classes for an existing database. It is free for non-commercial use.
Version 2.0 has a new user interface and adds possibility to configure the generated code in the DB Importer editor.
A script can be used to configure many values at once or for more advanced configurations. For instance:
- Filter which tables to import.
- Determine whether to generate relations for foreign keys.
- Control generation of many-to-many relations from join tables.
- Configure class and variable names. For instance, a table name like "tb_person_account" can be converted to a class name "PersonAccount".
- Generate annotations such as @GeneratedId, @Version, etc.
- Check database design rules.
Here are some examples of code that can be generated:
Generate @Version annotation on all fields with name "version"
@Version
private long version;
Generate @GeneratedValue(strategy=GenerationType.AUTO) on all PK fields
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
Convert all tinyint SQL columns to Boolean or boolean Java types
private boolean b;
Let some classes implement a certain interface
@Entity
class Person implements PersistentObject
Generate both relation and attribute for a field
This can be useful with frameworks such as SmartGWT, which don't handle JPA relations very well.
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="person_id", nullable=false,
insertable=false, updatable=false)
private Person person;
@Column(name="person_id")
private long personId;
Generate ordered lists for some collections
@OneToMany(mappedBy="messageList", fetch=FetchType.LAZY)
@OrderBy(value="sortOrder")
private List messages = new ArrayList();
Generate enum types instead of int for some fields
@Column(name="status", length=10)
private Status status;
Generate metadata
public interface PersonMetadata {
String ID = "id";
String NAME = "name";
}
Import unique constraints
@Entity
@Table(name="messages", uniqueConstraints={
@UniqueConstraint(columnNames={"unique_code"})
} )
See
our home page for how to do this in practice.