roger_that
roger_that

Reputation: 9791

Spring Data JPA with SQL Server giving error: Invalid object name 'Table_Name'

I am trying to save a list of object in a sql-server database in my Spring Boot application but getting exception when I call the save method.

2018-11-26 11:50:41.896  WARN 22082 --- [nio-8080-exec-2] o.h.e.j.s.SqlExceptionHelper             : SQL Error: 208, SQLState: S0002
2018-11-26 11:50:41.897 ERROR 22082 --- [nio-8080-exec-2] o.h.e.j.s.SqlExceptionHelper             : Invalid object name 'ticket'.

Below is the configuration am using:

#SQL Server configurations
spring.datasource.url=jdbc:sqlserver://my.server.ip.address;databaseName=UH
spring.datasource.username=Test
spring.datasource.password=Test@123
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect

And the Model looks like:

@Entity(name = "Ticket")
public class Ticket {

    public Ticket() {

    }

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @Column(name = "ticket_id", nullable = false, length=200)
    private long ticket_id;

    @Column(name = "topic", nullable = false, length=200)
    private String topic;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Set<Tag> tag;

    @Column(name = "type", nullable = false, length=200)
    private String type;

    @Column(name = "brand", nullable = false, length=200)
    private long brand;

    @Column(name = "ticket_group", nullable = false, length=200)
    private long ticket_group;

    @Column(name = "priority", nullable = false, length=200)
    private String priority;

    @Column(name = "status", nullable = false, length=200)
    private String status;


    @Column(name = "created_date", nullable = false, length=200)
    private String created_date;

    @Column(name = "created_time", nullable = false, length=200)
    private String created_time;

    @Column(name = "channel", nullable = false, length=200)
    private String channel;

    // Getters & Setters....

}

I have created the Repository class:

@Repository
public interface TicketRepository extends JpaRepository<Ticket, Long> {

}

and finally, the controller:

@RestController
public class TicketController {


  @Autowired
  TicketRepository ticketRepository;

  @GetMapping("/tickets")
  public void saveTicketData() {

      List<Ticket> tickets = null;
      try {
            tickets = getAllTickets();  //some utility method
            ticketRepository.save(tickets);  // here exception occurs
        } catch (Exception e) {
            e.printStackTrace();
        }   
}

Not sure what is wrong here. Is there a different way to save list of objects in SQL server than in MySQl? This works great with MySQL or if I switch to Mongo.

Any help would be appreciated.

Upvotes: 2

Views: 10641

Answers (4)

Tudor
Tudor

Reputation: 2706

In my case I missed the following setting:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

It was reformatting the Table name for some reason.

Upvotes: 4

Drashti Desai
Drashti Desai

Reputation: 1

In my case table is not created with JPA. We are created table externally and then all other operation like insert select was done. So , I am facing this issue because of table's absense

Upvotes: 0

Serhat Oz
Serhat Oz

Reputation: 798

Sqlserver is case sensitive so if the table name is TICKET it does not recognize "i" char in Ticket. Can you check if the table name is TICKET or ticket. if it is TICKET then try this annotation.

@Table(name = "TICKET")
@Entity(name = "TICKET")

Upvotes: 1

atul ranjan
atul ranjan

Reputation: 544

Include one more annotation @Table(name = "Ticket") in Entity Class.

@Table(name = "Ticket") @Entity(name = "Ticket") public class Ticket {.....

Upvotes: 1

Related Questions