简体   繁体   English

java spring 微服务中的 postgres 未显示数据

[英]Data are not diplayed from postgres in java spring microservice

Im trying to connect postgres with a spring boot microservice app.我试图将 postgres 与 spring 启动微服务应用程序连接起来。

Have make the configuration at application.propertiesapplication.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存储库 class

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

Resource class资源 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

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. Hibernate 将我的表 T_Item 保存到同一个数据库中但名称不同的 t_item,这就是为什么连接正常但表为空的原因。

More info:更多信息:

Hibernate and Spring modify query Before Submitting to DB Hibernate 和 Spring 在提交到数据库之前修改查询

Hibernate and Spring modify query Before Submitting to DB Hibernate 和 Spring 在提交到数据库之前修改查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM