2016-03-14 208 views
1

我用spring創建了項目。Spring jpa實體和Lombok

dependencies { 
    compile('org.springframework.boot:spring-boot-starter-cache') 
    compile('org.springframework.boot:spring-boot-starter-data-jpa') 
    compile('org.springframework.boot:spring-boot-starter-data-rest') 
    compile('org.springframework.boot:spring-boot-devtools') 
    compile('org.projectlombok:lombok') 
    compile('org.springframework.boot:spring-boot-starter-thymeleaf') 
    compile('org.springframework.boot:spring-boot-starter-web') 
    compile('org.codehaus.groovy:groovy') 
    runtime('com.h2database:h2') 
    runtime('mysql:mysql-connector-java') 
    testCompile('org.springframework.boot:spring-boot-starter-test') 
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc') 
} 

application.properties:

spring.data.rest.base-path=/api 

spring.datasource.url=jdbc:mysql://localhost/secret_backend 
spring.datasource.username=root 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 

spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect 
spring.jpa.show-sql=true 
spring.jpa.hibernate.ddl-auto=update 

與實體類:

package com.app.Entity 

import lombok.Data 

import javax.persistence.Entity 
import javax.persistence.GeneratedValue 
import javax.persistence.Id 
import javax.persistence.Table 

@Data 
@Entity 
@Table(name = "cities") 
public class City { 

    private @Id @GeneratedValue Long id; 
    private String slug; 
    private String title; 
    private String titleShort; 

    private City() {} 

    /*public String getSlug(){ 
     return slug; 
    }*/ 

    public City(String slug) { 
     this.slug = slug; 
    } 
} 

當我瀏覽到本地主機:8080/API /城市,我沒有看到實際的數據表格數據庫:

{ 
    "_embedded": { 
    "cities": [ 
     { 
     "_links": { 
      "self": { 
      "href": "http://localhost:8080/api/cities/7" 
      }, 
      "city": { 
      "href": "http://localhost:8080/api/cities/7" 
      } 
     } 
     }, 
     { 
     "_links": { 
      "self": { 
      "href": "http://localhost:8080/api/cities/8" 
      }, 
      "city": { 
      "href": "http://localhost:8080/api/cities/8" 
      } 
     } 
     }, 
... 

O如果我將getters添加到實體中,我會看到數據,但是從lombok文檔@Data註釋必須爲所有實體屬性生成getter和setter。

+0

您是否檢查實際輸出?龍目有沒有在班級上班?另外,lombok只需要編譯時間而不是運行時。 – highstakes

+0

實際產量? 也許一些信息爲java(春天)虛擬? 和lombok正在編譯('org.projectlombok:lombok') – Cawa

+0

我的意思是,檢查編譯後的.class文件,看看這些方法是否已經添加到它們中(使用java反編譯器或'javap'命令)。編譯依賴意味着它將被包含在生成的工件中,這將被提供作用域(你需要一個用於gradle的插件),儘管這並不重要。 – highstakes

回答

1

將City.groovy更名爲City.java,現在工作正常。感謝@highstakes