当您按下 ctrl+n 并选择“Persistance -> Genarate entity classes from Database”时,可以在 Netbeans 7.2 中找到此工具。它工作得很好。
我想在 IntelliJ 中使用它,因为 IntelliJ 使用不同的东西(持久性选项卡中的选项“生成持久网络映射 -> 按数据库模式”)从模式生成实体。生成的类没有用,因为它们只包含原始类型
例如:
我的架构的一部分:
create table users (
id int not null primary key,
login varchar(64) not null unique,
pass char(150) not null
);
create table groups (
login varchar(64) not null,
group_name varchar(64) not null,
foreign key (login) references users (login)
);
IntelliJ 生成的部分实体:
@Entity
public class Users
{
private Integer id;
@javax.persistence.Column(name = "id")
@Id
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
private String login;
@javax.persistence.Column(name = "login")
@Basic
public String getLogin()
{
return login;
}
public void setLogin(String login)
{
this.login = login;
}
private String pass;
@javax.persistence.Column(name = "pass")
@Basic
public String getPass()
{
return pass;
}
public void setPass(String pass)
{
this.pass = pass;
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Users users = (Users) o;
if (id != null ? !id.equals(users.id) : users.id != null) return false;
if (login != null ? !login.equals(users.login) : users.login != null) return false;
if (pass != null ? !pass.equals(users.pass) : users.pass != null) return false;
return true;
}
@Override
public int hashCode()
{
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (login != null ? login.hashCode() : 0);
result = 31 * result + (pass != null ? pass.hashCode() : 0);
return result;
}
}
NetBeans 生成的实体的一部分(我想在 IntelliJ 中使用的正确方式)
@Entity
@Table(name = "users", catalog = "soccerweb", schema = "public")
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findById", query = "SELECT u FROM Users u WHERE u.id = :id"),
@NamedQuery(name = "Users.findByLogin", query = "SELECT u FROM Users u WHERE u.login = :login"),
@NamedQuery(name = "Users.findByPass", query = "SELECT u FROM Users u WHERE u.pass = :pass")})
public class Users implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 64)
@Column(name = "login")
private String login;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 150)
@Column(name = "pass")
private String pass;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userId", fetch = FetchType.LAZY)
private List<Achievements> achievementsList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userId", fetch = FetchType.LAZY)
private List<BlueTeam> blueTeamList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userId", fetch = FetchType.LAZY)
private List<RedTeam> redTeamList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userId", fetch = FetchType.LAZY)
private List<TournamentPlayers> tournamentPlayersList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userId", fetch = FetchType.LAZY)
private List<Stats> statsList;
@OneToMany(mappedBy = "userId", fetch = FetchType.LAZY)
private List<TournamentStats> tournamentStatsList;
@OneToMany(mappedBy = "userId", fetch = FetchType.LAZY)
private List<TournamentAchievements> tournamentAchievementsList;