Java CRUDepository保存方法未保存到oracle数据库
我正在尝试创建一个应用程序,使用Crudepository将数据保存到Oracle数据库中。这是我的寄存处:Java CRUDepository保存方法未保存到oracle数据库,java,database,spring,oracle,spring-data-jpa,Java,Database,Spring,Oracle,Spring Data Jpa,我正在尝试创建一个应用程序,使用Crudepository将数据保存到Oracle数据库中。这是我的寄存处: public interface CustomerRepository extends CrudRepository<Customer, Long> { List<Customer> findByEmail(String email); List<Customer> findByDate(Date date); // cu
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);
}
而我的客户实体类是:
@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);
}
}
}
我没有看到新客户添加到SOP或数据库中。这里我遗漏了什么?您的问题似乎是在只读事务中执行
save
语句。解决方案可以简单到删除该属性
读取readOnly
标志时,它表示:
如果事务实际上是只读的,则可以将布尔标志设置为true,从而允许在运行时进行相应的优化
仅使用@事务性:
@Transactional
@Override
public void run(String... args) throws Exception {
// the rest of your code ...
}
代码运行得很好。
只是在我的应用程序代码中,我按照我的旧代码更改了application.property文件,并将“spring.datasource.url”改为“appname.datasource.url”,这就是代码从未与DB交互的原因。是否有错误?您调试了代码吗?没有事务,没有保存到数据库。您试图在只读事务中保存,我认为这可能是一个问题。我参考了以下链接:执行了步骤,并且能够写入数据库。没有使用任何@transactional。我真的需要用它吗?然而,在我的原始代码中,当尝试同样的事情时,仍然没有写入DB。严格地说,对于您的情况,您实际上根本不需要@transnational
。您可以删除它。但是你有错误吗?也许我们可以调试它为什么不工作。
@Transactional
@Override
public void run(String... args) throws Exception {
// the rest of your code ...
}