I have a MySQL database, with some data, that I want to be transformed into java Entity classes. I have my Persistence.xml file, but I'm not sure how I can do this. I could reverse engineer it, and make a data export from my MySQL workbench, but after I have made the connection, with the database, can I use a function within Netbeans to accomplish this?
1 Answer
NetBeans has a wizard to do exactly what you want:
- In the Projects panel, within your project select the package to contain the entity class(es).
Right click and select New > Entity Classes from Database... from the context menu.
The New Entity Classes from Database wizard runs.
On the Database Tables page:
- Select the connection you want to use from the Database Connection drop list.
- Select the table(s) you want to generate entity classes for from the Available Tables list.
- Click the Add > or Add All > button to move the table(s) to the Selected Tables list, then click the Next > button.
- On the Entity Classes page select any options you want, though the defaults will frequently be appropriate. Click the Next > button.
- On the Mapping Options page select any options you want, though the defaults will frequently be appropriate. Click the Finish button.
After running the wizard the entity class(es) will be created within the package you selected. For example:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mysql8demo;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author johndoe
*/
@Entity
@Table(name = "country")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Country.findAll", query = "SELECT c FROM Country c")
, @NamedQuery(name = "Country.findById", query = "SELECT c FROM Country c WHERE c.id = :id")
, @NamedQuery(name = "Country.findByCountryCode", query = "SELECT c FROM Country c WHERE c.countryCode = :countryCode")
, @NamedQuery(name = "Country.findByCountryName", query = "SELECT c FROM Country c WHERE c.countryName = :countryName")
, @NamedQuery(name = "Country.findByCurrencyCode", query = "SELECT c FROM Country c WHERE c.currencyCode = :currencyCode")
, @NamedQuery(name = "Country.findByPopulation", query = "SELECT c FROM Country c WHERE c.population = :population")
, @NamedQuery(name = "Country.findByCapital", query = "SELECT c FROM Country c WHERE c.capital = :capital")
, @NamedQuery(name = "Country.findByContinent", query = "SELECT c FROM Country c WHERE c.continent = :continent")
, @NamedQuery(name = "Country.findByAreaInSqKm", query = "SELECT c FROM Country c WHERE c.areaInSqKm = :areaInSqKm")})
public class Country implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@Column(name = "countryCode")
private String countryCode;
@Basic(optional = false)
@Column(name = "countryName")
private String countryName;
@Column(name = "currencyCode")
private String currencyCode;
@Column(name = "population")
private String population;
@Column(name = "capital")
private String capital;
@Column(name = "continent")
private String continent;
@Column(name = "areaInSqKm")
private Integer areaInSqKm;
public Country() {
}
public Country(Integer id) {
this.id = id;
}
public Country(Integer id, String countryCode, String countryName) {
this.id = id;
this.countryCode = countryCode;
this.countryName = countryName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getCurrencyCode() {
return currencyCode;
}
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
}
public String getPopulation() {
return population;
}
public void setPopulation(String population) {
this.population = population;
}
public String getCapital() {
return capital;
}
public void setCapital(String capital) {
this.capital = capital;
}
public String getContinent() {
return continent;
}
public void setContinent(String continent) {
this.continent = continent;
}
public Integer getAreaInSqKm() {
return areaInSqKm;
}
public void setAreaInSqKm(Integer areaInSqKm) {
this.areaInSqKm = areaInSqKm;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Country)) {
return false;
}
Country other = (Country) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "mysql8demo.Country[ id=" + id + " ]";
}
}
1 Comment
Kristoffer Tølbøll
This was exactly the answer I was looking for. Thank you

