如何从表a中获取插入的ID,并将该ID用于插入表b。

huangapple go评论107阅读模式
英文:

How to get the inserted id from table a and use the id to insert into table b

问题

我想在表a中保存一条记录并获取插入的id;然后将这个id保存到表b中的另一条记录中。

在Service中:

public Long save(Entity entity, String a) {
    entity = entityRepository.saveAndFlush(entity);
    Long insertedEntityId = entity.getId();
    EntityB entityb = new EntityB(
        insertedEntityId,
        a);
    entityb = entityBRepository.saveAndFlush(entityb);
    return entityb.getId();
}

在EntityB中:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
public Long getId() {
    return id;
}

然而,我遇到了以下错误:

com.microsoft.sqlserver.jdbc.SQLServerException: 无法将值NULL插入列'id',表'...'; 列不允许为空。INSERT 失败。

如何解决这个问题?

我使用的是SQL Server 2012,Spring Boot 2.3,Java 11。

非常感谢!

英文:

I would like to save a record in table a and get the inserted id; and get this id to save into another record in table b.

In Service:

public Long save(Entity entity, String a) {
	entity = entityRepository.saveAndFlush(entity);
	Long insertedEntityId = entity.getId();
	EntityB entityb = new EntityB(
		insertedEntityId,
		a);
	entityb = entityBRepository.saveAndFlush(entityb);
	return entityb.getId();
}

In EntityB:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
public Long getId() {
	return id;
}

However, I got the error:

com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'id', table '...'; column does not allow nulls. INSERT fails.

How to fix this problem?

I'm using SQL server 2012, Spring boot 2.3, Java 11.

Many thanks!

答案1

得分: 3

你必须创建一个构造函数,接受两个参数,就像在下面的代码行中所做的那样:

EntityB entityb = new EntityB(insertedEntityId, a);

在那个构造函数中,假设类似这样:

public EntityB(Long id, String param) {
    this.id = id;
    this.param = param;
}

这部分代码:

public Long getId() {
    return id;
}

不正确,它只是用于获取 id 的 getter 方法。希望能正常工作!

英文:

You have to create constructor receive two parameters as you do in the lines:

EntityB entityb = new EntityB(insertedEntityId, a);

In that constructor, assume something like this:

public EntityB(Long id, String param) {
        this.id = id;
        this.param = param;
    }

The lines

public Long getId() {
    return id;
}

are not correct, it is just the getter method using for retrieving id. Hope it works!

答案2

得分: 0

使用GenerationType.IDENTITY时,该列必须是SQL Server中的identity列(即自增列)。当使用GenerationType.IDENTITY时,身份标识由数据库服务器管理,因此您的应用程序不会为id列发送任何值。如果在SQL Server中未将该列定义为identity,但将其定义为NOT NULL,则会出现您所看到的错误。

此外,尽管可能不太需要,但在public Long getId()之前添加此行private long id;,以明确确保装饰适用于id

英文:

When using GenerationType.IDENTITY, the column must be an identity column in SQL Server (i.e. auto-increment). When GenerationType.IDENTITY is used, the identity is managed by the database server and thus your app does not send any value for the id column. If the column is not defined as identity in SQL Server but defined as NOT NULL, then you will get the error you see.

Also, unlikely needed, but add this line private long id; before public Long getId() to explicitly make sure the decoration applies to id.

huangapple
  • 本文由 发表于 2020年8月17日 11:15:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63444097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定