没有把 Java 字段命名为与表列完全相同的名称。在 JPA Structure 窗口中选择 firstName 字段。在 JPA Details 的下拉列表中选择相 应的列。
图 43. 把 firstName 映射到 First_Name
对 lastName 字段执行相同的操作。再执行一次 JPA 同步,所有问题都应当 会解决。应当会更新 Player 类的代码,如清单 7 所示:
清单 7. 完成的 Player 类
package org.developerworks.baseball;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Column;
@Entity
@Table(schema="baseball", name = "Players")
public class Player {
@Id
@GeneratedValue(strategy=IDENTITY)
private int id;
@Column(name="First_Name")
private String firstName;
@Column(name="Last_Name")
private String lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
用Eclipse Europa进行Web开发,第1部分 - Eclipse Java EE(15)
时间:2010-11-29 IBM Michael Galpin
现在可以执行相同的过程并创建映射到 Games 表的 Game 类。Game 类的代 码如清单 8 所示:
清单 8. Game 类
package org.developerworks.baseball;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Column;
@Entity
@Table(schema="baseball", name = "Games")
public class Game {
@Id
@GeneratedValue(strategy=IDENTITY)
private int id;
@ManyToOne(optional=false)
@JoinColumn(name="Player_Id", nullable=false, updatable=false)
private Player player;
@Column(name="H")
private int hits;
@Column(name="2B")
private int doubles;
@Column(name="3B")
private int triples;
@Column(name="HR")
private int homeRuns;
@Column(name="BB")
private int walks;
@Column(name="R")
private int runs;
@Column(name="RBI")
private int rbis;
@Column(name="AB")
private int atBats;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Player getPlayer() {
|