我正在尝试创建一个应用程序,以使用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);
}
}
}
我看不到在肥皂或数据库中添加的新客户。我在这里想念什么?
答案 0 :(得分:1)
您的问题似乎是您在 readOnly 事务中执行save
语句。解决方案可能就像删除该属性一样简单。
读取readOnly
标志documentation,它指出:
如果事务实际上是只读的,则可以将其设置为true的布尔值标志,从而允许在运行时进行相应的优化。
仅使用@Transactional
:
@Transactional
@Override
public void run(String... args) throws Exception {
// the rest of your code ...
}
答案 1 :(得分:0)
代码工作正常。 只是在我的应用程序代码中,我已按照旧代码更改了application.property文件,而不是“ appname.datasource.url”,而不是“ spring.datasource.url”,这就是为什么代码从未与DB交互的原因