gpt4 book ai didi

java - JPA @MapsId 插入不正确的列名

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:05:40 26 4
gpt4 key购买 nike

尽管遵循了网上的所有示例,但我还是无法让@MapsId 正常工作。

这是我的设置:

pom.xml:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>

DDL 脚本:

CREATE TABLE Subscription (
id bigint not null primary key auto_increment,
active boolean not null,
maxNumUsers int not null
)ENGINE=InnoDB;

CREATE TABLE t.Project (
name varchar(32) not null,
subscriptionId bigint not null
)ENGINE=InnoDB;

ALTER TABLE t.Project ADD CONSTRAINT `projectPK` PRIMARY KEY (name, subscriptionId);
ALTER TABLE t.Project ADD CONSTRAINT `projectSubscriptionFK` FOREIGN KEY (subscriptionId) REFERENCES t.Subscription(id);

项目编号.java:

@Embeddable
public class ProjectId implements Serializable {
private Long subscriptionId;
private String name;

项目.java:

@Entity
@Table(schema = "t")
public class Project {
@EmbeddedId
private ProjectId id;

@ManyToOne
@MapsId("subscriptionId")
//@JoinColumn(name = "subscriptionId", referencedColumnName = "id", insertable = false, updatable = false)
private Subscription subscription;

订阅.java:

@Entity
@Table(schema = "t")
public class Subscription {

@OneToMany(mappedBy = "subscription", cascade = CascadeType.ALL)
private Map<ProjectId, Project> projects = new HashMap<>();

当我插入时,出现以下错误:

Hibernate: insert into t.Project (name, subscription_id) values (?, ?) 2014-07-20 11:06:59,193 org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1054, SQLState: 42S22 2014-07-20 11:06:59,193 org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unknown column 'subscription_id' in 'field list'

...

... 94 more Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'subscription_id' in 'field list' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at com.mysql.jdbc.Util.handleNewInstance(Util.java:408) at com.mysql.jdbc.Util.getInstance(Util.java:383) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1062) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4226) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4158) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2840) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187) ... 114 more

如果我在 Project.subscription 上取消注释 @JoinColumn 并重新部署,我会收到此错误:

... 21 more Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: t.Subscription.projects column: subscriptionId

我也试过删除 @MapsId,我仍然收到重复列错误。

我不知道如何解决这个问题。如有任何建议,我们将不胜感激。

谢谢


更新:如果我将 Subscription.projects 设为列表而不是 map ,并将 JoinColumn 添加到 project.subscription,它就可以工作。但是,如果我将 subscription.projects 改回 map ,我将再次开始获取 org.hibernate.MappingException: Repeated 列。显然,我真的很想使用 map ,所以我会继续挖掘。

最佳答案

当使用 mapsId 注释时,您指定关系共享和控制由 Id 映射映射的字段,因此关系映射中的字段覆盖 Id 映射中的字段。这意味着 JPA 将使用 subscription_id 默认值,除非您指定您注释掉的连接列:

@JoinColumn(name = "subscriptionId", referencedColumnName = "id")

关于java - JPA @MapsId 插入不正确的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24871676/

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