【问题标题】:CrudRepository save method not saving to oracle databseCrudRepository 保存方法不保存到 oracle 数据库
【发布时间】:2020-06-09 08:02:36
【问题描述】:

我正在尝试创建一个应用程序来使用 CrudRepository 将数据保存到 Oracle 数据库中。这是我的存储库:

public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findByEmail(String email);

    List<Customer> findByDate(Date date);

    // custom query example and return a stream
    @Query("select c from Customer c where c.email = :email")
    Stream<Customer> findByEmailReturnStream(@Param("email") String email);

}

我的 application.property 看起来像:

spring.datasource.url=jdbc:oracle:thin:@vgdevst-scan.hhs.local:1521/EONDEV.hhslocal
spring.datasource.username=EON_USER
spring.datasource.password=EON_USERD
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver

虽然我的客户实体类是:

@Entity
public class Customer {

//http://www.oracle.com/technetwork/middleware/ias/id-generation-083058.html
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CUST_SEQ")
@SequenceGenerator(sequenceName = "customer_seq", initialValue = 1, allocationSize = 1, name = "CUST_SEQ")
Long id;

String name;
String email;

//@Temporal(TemporalType.DATE)
@Column(name = "CREATED_DATE")
Date date;

public Customer(String name, String email, Date date) {
    this.name = name;
    this.email = email;
    this.date = date;
}

public Customer(Long id, String name, String email, Date date) {
    super();
    this.id = id;
    this.name = name;
    this.email = email;
    this.date = date;
}

public Customer() {
}

@Override
public String toString() {
    return "Customer{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", email='" + email + '\'' +
            ", date=" + date +
            '}';
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

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

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

我正在尝试使用以下方法将新客户保存到数据库:

@SpringBootApplication
public class Application implements CommandLineRunner {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    DataSource dataSource;

    @Autowired
    CustomerRepository customerRepository;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Transactional(readOnly = true)
    @Override
    public void run(String... args) throws Exception {

        System.out.println("DATASOURCE = " + dataSource);
        customerRepository.save(new Customer(new Long(4),"Amit","a.r@state.ma.us",new Date()));
        System.out.println("\n1.findAll()...");
        for (Customer customer : customerRepository.findAll()) {
            System.out.println(customer);
        }
    }
}

我没有看到在 sops 或数据库中添加了新客户。我在这里错过了什么?

【问题讨论】:

  • 您是否收到错误消息?你调试过你的代码吗?
  • 没有事务,没有保存到数据库。
  • 您在只读事务中尝试保存,我认为这可能是个问题。

标签: java database spring oracle spring-data-jpa


【解决方案1】:

您的问题似乎是您在 readOnly 事务中执行save 语句。解决方案可以像删除该属性一样简单。

阅读readOnly标志documentation,它指出:

如果事务实际上是只读的,则可以设置为 true 的布尔标志,允许在运行时进行相应的优化。

仅使用@Transactional:

@Transactional
@Override
public void run(String... args) throws Exception {
    // the rest of your code ...
}

【讨论】:

  • 我参考了以下链接:dzone.com/articles/spring-boot-jpa-hibernate-oracle 进行了分步实施并且能够写入数据库。没有使用任何@transactional。我真的需要使用它吗?然而,在我的原始代码中,当尝试同样的事情时,仍然没有写入数据库。
  • 严格来说,您实际上根本不需要@Transcational。你可以删除它。但是你有什么错误吗?也许我们可以调试它为什么不工作。
【解决方案2】:

代码运行良好。 只是在我的应用程序代码中,我根据旧代码更改了 application.property 文件,而不是“spring.datasource.url”,我放了“appname.datasource.url”,这就是为什么代码从未与数据库交互.

【讨论】:

    猜你喜欢
    • 2020-07-03
    • 2021-10-05
    • 2020-04-02
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多