gpt4 book ai didi

java - 在多对多关系中收到 org.hibernate.id.IdentifierGenerationException

转载 作者:行者123 更新时间:2023-12-01 14:11:07 25 4
gpt4 key购买 nike

为了快速生产,我正在尝试使用 JBoss Tools 4.1 Hibernate 插件。但我在第一回合遇到了异常(exception)。

首先物理数据库表如下:

 describe Teams ; 
Name Null Type
ID NOT NULL NUMBER
NAME NOT NULL VARCHAR2(20)
COUNTRY NOT NULL VARCHAR2(20)

describe players;
Name Null Type
ID NOT NULL NUMBER
NAME NOT NULL VARCHAR2(20)
NATIONALITY NOT NULL VARCHAR2(20)

describe player_team;
Name Null Type
PLAYER_ID NOT NULL NUMBER
TEAM_ID NOT NULL NUMBER

实体如下:

@Entity
@Table(name = "PLAYERS")
public class Players implements java.io.Serializable {

private BigDecimal id;
private String name;
private String nationality;
private Set<PlayerTeam> playerTeams = new HashSet<PlayerTeam>(0);

@Id
@Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
public BigDecimal getId() {
return this.id;
}

@Column(name = "NAME", nullable = false, length = 20)
public String getName() {
return this.name;
}

@Column(name = "NATIONALITY", nullable = false, length = 20)
public String getNationality() {
return this.nationality;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "players")
public Set<PlayerTeam> getPlayerTeams() {
return this.playerTeams;
}
// setters go here but i deleted for readability

}


@Entity
@Table(name = "TEAMS")
public class Teams implements java.io.Serializable {

private BigDecimal id;
private String name;
private String country;
private Set<PlayerTeam> playerTeams = new HashSet<PlayerTeam>(0);

@Id
@Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
public BigDecimal getId() {
return this.id;
}

@Column(name = "NAME", nullable = false, length = 20)
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

@Column(name = "COUNTRY", nullable = false, length = 20)
public String getCountry() {
return this.country;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "teams")
public Set<PlayerTeam> getPlayerTeams() {
return this.playerTeams;
}

// setters go here

}

@Embeddable
public class PlayerTeamId implements java.io.Serializable {

private BigDecimal playerId;
private BigDecimal teamId;

@Column(name = "PLAYER_ID", nullable = false, precision = 22, scale = 0)
public BigDecimal getPlayerId() {
return this.playerId;
}

public void setPlayerId(BigDecimal playerId) {
this.playerId = playerId;
}

@Column(name = "TEAM_ID", nullable = false, precision = 22, scale = 0)
public BigDecimal getTeamId() {
return this.teamId;
}

public void setTeamId(BigDecimal teamId) {
this.teamId = teamId;
}

public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof PlayerTeamId))
return false;
PlayerTeamId castOther = (PlayerTeamId) other;

return ((this.getPlayerId() == castOther.getPlayerId()) || (this
.getPlayerId() != null && castOther.getPlayerId() != null && this
.getPlayerId().equals(castOther.getPlayerId())))
&& ((this.getTeamId() == castOther.getTeamId()) || (this
.getTeamId() != null &&
castOther.getTeamId() != null && this
.getTeamId().equals(castOther.getTeamId())));
}

public int hashCode() {
int result = 17;

result = 37 * result
+ (getPlayerId() == null ? 0 :
this.getPlayerId().hashCode());
result = 37 * result
+ (getTeamId() == null ? 0 : this.getTeamId().hashCode());
return result;
}

}

然后我尝试在连接表中插入记录,如下所示:

            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.getTransaction().begin();
Players pl = (Players) session.load(Players.class, new BigDecimal(2));
Teams tm = (Teams) session.load(Teams.class, new BigDecimal(25));
PlayerTeam pt = new PlayerTeam();
pt.setPlayers(pl);
pt.setTeams(tm);
session.save(pt);
session.getTransaction().commit();

最终出现以下异常:

 org.hibernate.id.IdentifierGenerationException: null id generated for:class    
business.PlayerTeam

最后是hibernate.cfg.xml

  <session-factory name="se">
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property
name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.password">LECTURE1</property>
<property
name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">lecture1</property>
<property name="hibernate.default_schema">LECTURE1</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle8iDialect</property>
<mapping class="business.Players" />
<mapping class="business.PlayerTeam" />
<mapping class="business.Teams" />
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
</session-factory>

Would you throw some light on this please ? thanks

最佳答案

Hibernate 中的每个实体都需要一个用 @Id 注释的 ID 列。您的 PlayerTeam 类没有这样的列。如果您的 PlayerTeam 类只是一个连接表,它甚至不需要是一个实体。您可以简单地执行以下操作:

public class Players {

...

@ManyToMany(mappedBy="players", fetch=FetchType.LAZY)
private List<Teams> teams;

...

}

public class Teams {

...

@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="player_team", joinColumns={@JoinColumn(name="TEAM_ID")}, inverseJoinColumns={@JoinColumn(name="PLAYER_ID")})
private list<Players> players;

...

}

这将与作为拥有方的 Teams 类建立双向多对多关系。然后,您可以完全删除 PlayerTeamId 类。

顺便说一句,我强烈建议您的类(class)名称采用单数(例如“玩家”和“团队”,而不是“玩家和团队”)

关于java - 在多对多关系中收到 org.hibernate.id.IdentifierGenerationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18515401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com