2

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.

5
  • Can you also show your utility method. Commented Nov 26, 2018 at 7:00
  • erm, can screenshot your database schema/table? just curious that whether you put the name as "db"... Commented Nov 26, 2018 at 7:21
  • edited the db name. That's exactly what I have created in SQl server Commented Nov 26, 2018 at 7:26
  • @PoojaAggarwal: Utility method is nothing but parses some JSON and created Ticket object, populates ticket fields and save it the a set and returns. Commented Nov 26, 2018 at 7:27
  • One thing to keep in mind while working with sql server is that sql server uses schema, while mysql doesn't. It might not be directly applicable here but if you use nativeQuery, ensure table name in query is of type dbName.schemaName.tableName. Commented Oct 11, 2024 at 10:41

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

hello savior. Not sure why this is not mentioned anywhere in the docs
1

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

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

2 Comments

What is the table name in db? Depending upon OS the SQL Server table names may be case sensitive. So, please check the table name is small case ticket or Ticket. It must be ticket.
It is hosted on windows. table name is ticket
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.