弹簧数据中的@Transient不起作用

Mel*_*ius 3 java spring hibernate jpa spring-data

我有Settlement实体

@Entity
@Table(name = "settlement")
public class Settlement {

    @ManyToOne
    @JoinColumn(name = "subscription_x_product_id")
    private ProductSubscription productSubscription;
Run Code Online (Sandbox Code Playgroud)

ProductSubscription实体有关

@Entity
@Table(name = "subscriptionproduct")
public class ProductSubscription {
    @ManyToOne
    @JoinColumn(name = "product_id")
    private Product product;
Run Code Online (Sandbox Code Playgroud)

Product实体有关

@Entity
public class Product {
    @Transient
    private String enabled;
Run Code Online (Sandbox Code Playgroud)

Product实体中我有enabled一个注释的字段@org.springframework.data.annotation.Transient.我也有存储库

public interface SettlementRepository extends JpaRepository<Settlement, Integer>
Run Code Online (Sandbox Code Playgroud)

当我打电话给SettlementRepository.findAll(); 它时给予例外Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'enabled'.

如何忽略enabled从DB加载的字段?

Mel*_*ius 7

我找到了解决方案,问题出现在Annotation中,@org.springframework.data.annotation.Transient一旦我改变@javax.persistence.Transient它就可以了.

  • 我有同样的问题,但我从一开始就使用“@javax.persistence.Transient”中的“@Transient”,但仍然没有成功。我保存实体并在返回的对象上调用 get 方法,并且该值被保留。你还发现别的东西吗?谢谢。 (2认同)
  • 对于进一步的读者来说,注释实际上是有效的,但是当我在“@Transactional”测试中使用它时,spring存储库似乎没有更新对象(保存的对象和从存储库返回的对象具有相同的对象ID) 。因此,该字段并未写入数据库,但存储库仍在返回它,即使我调用 findOne 并将其分配给新变量也是如此。还是不知道如何解决。 (2认同)