0

Im trying to connect postgres with a spring boot microservice app.

Have make the configuration at application.properties

# spring.application.name=item-all-service
server.port = 8081
    spring.datasource.url=jdbc:postgresql://localhost:5432/xxx
    spring.jpa.properties.hibernate.default_schema = public
    spring.datasource.username=xxx
    spring.datasource.password=xx
    spring.jpa.show-sql=true
    spring.datasource.driverClassName=org.postgresql.Driver
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
    spring.jpa.hibernate.ddl-auto = update
    package com.example.itemsallservice.model;

    import java.io.Serializable;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @Entity
    @Table(name = "T_Item")
    public class ItemsModel implements Serializable {

        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        public long itemId;

        @Column(name = "nameId")
        private String nameId;

        public ItemsModel(long i, String nameId) {
            super();
            this.itemId = i;
            this.nameId = nameId;
        }

        public ItemsModel() {}

        public long getItemId() {
            return itemId;
        }
        public void setItemId(long itemId) {
            this.itemId = itemId;
        }
        public String getNameId() {
            return nameId;
        }
        public void setNameId(String nameId) {
            this.nameId = nameId;
        }

        @Override
        public String toString() {
            return "ItemsModel [itemId= \t " + itemId + " \t nameId=" + nameId + "] \n";
        }


    }

The repository class

@Repository
public interface ItemRepository  extends JpaRepository<ItemsModel, Long> {

Resource class

@RestController
@RequestMapping("/item")
public class ItemResource {

    @Autowired
    public ItemRepository itemRepo; 


       @GetMapping("/all")
        public String getAllEmployees() {
           String result = "";

        //   Query query = sessionFactory.getCurrentSession().createQuery("from Employee where email = :email");

            for(ItemsModel cust: itemRepo.findAll()  ){
              result += cust.toString() + "<br>"; 
            }

            System.out.println("itemModel" + result);
            return result;
        }

The result is empty

Hibernate: select itemsmodel0_.item_id as item_id1_0_, itemsmodel0_.name_id as name_id2_0_ from public.t_item itemsmodel0_
itemModel
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Stefani Toto
  • 280
  • 3
  • 13
  • what do you have in database? – dawidklos Nov 19 '19 at 10:10
  • What operation do you want to perform through database ? – Adya Nov 19 '19 at 10:20
  • 1
    It connects, it executes a query, but there is just nothing in the database. The result of an empty table is, no results. – M. Deinum Nov 19 '19 at 10:20
  • hava some data. the statement below gives data SELECT "itemId", "nameId" FROM public."T_Item"; – Stefani Toto Nov 19 '19 at 10:21
  • What is there in your Repository @Stefani Totokocopullo? – Adya Nov 19 '19 at 10:24
  • Only this @Repository public interface ItemRepository extends JpaRepository { – Stefani Toto Nov 19 '19 at 10:25
  • copy the hibernate sql into your SQL client, does it work? – Viet Nov 19 '19 at 10:28
  • Nope. Hibernate : select itemsmodel0_.item_id as item_id1_0_, itemsmodel0_.name_id as name_id2_0_ from public.t_item itemsmodel0_itemModel SQL should be :SELECT "itemId", "nameId" FROM public."T_Item"; – Stefani Toto Nov 19 '19 at 10:32
  • When you do select * from T_Item; WHat data do you get in postGres ? – Adya Nov 19 '19 at 10:33
  • SELECT * FROM public."T_Item"; it gives me data from table – Stefani Toto Nov 19 '19 at 10:33
  • try to add spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy – Viet Nov 19 '19 at 10:47
  • spring.jpa.hibernate.naming-strategy' is an unknown property. Did you mean 'spring.jpa.hibernate.naming.implicit-strategy'? – Stefani Toto Nov 19 '19 at 10:57
  • If the query would contain illegal column names it would give an error. It doesn't it simply doesn't return a result, hence the table that hibernate is using doesn't contain any data. So either you are connection to a different database or the table you are querying isn't the table that hibernate is using. – M. Deinum Nov 19 '19 at 11:07
  • The connection is like this in postgres: localhost > Databases (classwork) > Schemas > public > Tables (T_Item) spring.datasource.url=jdbc:postgresql://localhost:5432/classwork spring.jpa.properties.hibernate.default_schema = public – Stefani Toto Nov 19 '19 at 11:10

1 Answers1

0

Somehow solved.

Hibernate its saving my table, T_Item into the same db but with different name t_item, thats why connection its ok but table its empty.

More info:

Hibernate and Spring modify query Before Submitting to DB

Hibernate and Spring modify query Before Submitting to DB

Stefani Toto
  • 280
  • 3
  • 13