user3657868
user3657868

Reputation: 59

CrudRepository save method not saving to oracle databse

I am trying to create an application to save data into the Oracle database using CrudRepository. Here is my repositiry:

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);

}

My application.property looks like:

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

While my customer entity class is :

@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;
}

I am trying to save a new cutomer to database using:

@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","[email protected]",new Date()));
        System.out.println("\n1.findAll()...");
        for (Customer customer : customerRepository.findAll()) {
            System.out.println(customer);
        }
    }
}

I do not see the new customer added either in sops or in database. What am i missing here?

Upvotes: 1

Views: 1520

Answers (2)

user3657868
user3657868

Reputation: 59

The code was working just fine. Its just that in my application code, i had changed the application.property file as per my old code and instead of "spring.datasource.url" i had put "appname.datasource.url", which is why code never interacted with DB.

Upvotes: 0

Alexandru Somai
Alexandru Somai

Reputation: 1405

Your problem seems to be that you are executing the save statement in a readOnly transaction. The solution could be as simple as removing that property.

Reading the readOnly flag documentation, it states that:

A boolean flag that can be set to true if the transaction is effectively read-only, allowing for corresponding optimizations at runtime.

Use only @Transactional:

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

Upvotes: 1

Related Questions