英文:
SQLSyntaxErrorException when trying to save Entity with Composite Key
问题
以下是翻译好的内容:
这些是我的类:
UserActivity
@Entity
@Data
@IdClass(UserActivityId.class)
public class UserActivity {
@ManyToOne
private User user;
@Id
@Column(name = "user_id", insertable = false, updatable = false)
private Long userId;
@ManyToOne
private Stream stream;
@Id
@Column(name = "stream_id", insertable = false, updatable = false)
private Long streamId;
@Id
private String userIp;
//...还有8个字段
}
UserActivityId
@Data
public class UserActivityId implements Serializable {
private Long userId;
private Long streamId;
private String userIp;
//构造函数
}
Stream
@Entity
@Data
public class Stream {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "stream")
private List<UserActivity> UserActivities = new ArrayList<>();
}
User
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
protected long id;
}
UserActivityRepository
public interface UserActivityRepository extends JpaRepository<UserActivity, UserActivityId> {
}
但是当我尝试像这样保存一个实体:
var stream = streamRepository.findById(activity.getStreamId);
var user = userRespository.findById(activity.getUserId);
activity.setStream(stream);
activity.setUser(user);
userActivityRepository.save(activity);
我会得到一个异常,类似于这样的:
java.sql.SQLSyntaxErrorException: (conn=1058) Could not set parameter at position 12 (values was 1)
Query - conn:1058(M) - "insert into user_activity (a, b, c, d, e, f, g, h, user_id, stream_id, user_ip) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
这很奇怪,因为我只有11个字段,但 Hibernate 却试图设置第12个参数。我尝试重新创建数据库,问题仍然存在,但是当我移除复合键时,它可以正常工作。
英文:
these are my classes
UserActivity
@Entity
@Data
@IdClass(UserActivityId.class)
public class UserActivity {
@ManyToOne
private User user;
@Id
@Column(name = "user_id", insertable = false, updatable = false)
private Long userId;
@ManyToOne
private Stream stream;
@Id
@Column(name = "stream_id", insertable = false, updatable = false)
private Long streamId;
@Id
private String userIp;
//...8 more fields
}
UserActivityId
@Data
public class UserActivityId implements Serializable {
private Long userId;
private Long streamId;
private String userIp;
//constructors
}
Stream
@Entity
@Data
public class Stream {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "stream")
private List<UserActivity> UserActivities = new ArrayList<>();
}
User
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
protected long id;
}
UserActivityRepository
public interface UserActivityRepository extends JpaRepository<UserActivity, UserActivityId>
}
but when i try to save an entity like this :
var stream = streamRepository.findById(activity.getStreamId);
var user = userRespository.findById(activity.getUserId);
activity.setStream(stream);
activity.setUser(user);
userActivityRepository.save(activity);
i get and exception like this:
java.sql.SQLSyntaxErrorException: (conn=1058) Could not set parameter at position 12 (values was 1)
Query - conn:1058(M) - "insert into user_activity (a, b, c, d, e, f, g, h, user_id, stream_id, user_ip) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
which is strange because i only have 11 fields and hibernate is trying to set the 12th parameter
i tried recreating the database and the problem persisted, but when i remove the composite key it works
答案1
得分: 0
insertable=false, updatable=false
的定义在需要通过 OneToOne
或 ManyToOne
映射在实体中多次映射字段时非常有用。
我认为要解决这个问题,你需要从 Column
注解中移除 insertable=false, updatable=false
。
英文:
Defining insertable=false, updatable=false
is useful when you need to map a field more than once in an entity through a OneToOne
or ManyToOne
mapping.
I think to fix this issue you need to remove insertable=false, updatable=false
from the Column
annotation.
答案2
得分: 0
因此,我最终找到了问题所在,我需要将@id
放在关系引用(Stream、User)之上,而不是它们的外键上,另外我还需要为它们添加@MapsId
。
@ManyToOne
@Id
@MapsId
private User user;
@Column(name = "user_id", insertable = false, updatable = false)
private Long userId;
@ManyToOne
@Id
@MapsId
private Stream stream;
@Column(name = "stream_id", insertable = false, updatable = false)
private Long streamId;
@Id
private String userIp;
英文:
So i finally out found what the problem was,i had to place @id
above the relationship references (Stream, User) instead of their foreign keys, also i needed to add @MapsId
to them
@ManyToOne
@Id
@MapsId
private User user;
@Column(name = "user_id", insertable = false, updatable = false)
private Long userId;
@ManyToOne
@Id
@MapsId
private Stream stream;
@Column(name = "stream_id", insertable = false, updatable = false)
private Long streamId;
@Id
private String userIp;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论