简体   繁体   English

带有引导字段的Spring-boot + Hibernate + JPA

[英]Spring-boot + Hibernate + JPA with transient fields

This is my first question, but I have been looking for a solution for 2 days, with no success. 这是我的第一个问题,但是我一直在寻找解决方案已有2天,但没有成功。 In my project, I have a User entity with a transient property: 在我的项目中,我有一个带有过渡属性的User实体:

@Transient
@JsonProperty
private List<String> files;

I have no setter, and getter is: 我没有二传手,而getter是:

public List<String> getFiles() {
    /* Call one static method */
}

Executing the application with NetBeans in debug, works fine, and from javascript, I can get the getFiles results, using user.fotos . 使用NetBeans在调试中执行应用程序可以正常工作,并且可以从user.fotos使用user.fotos获得getFiles结果。 But when I generate .jar file, and I execute the application with command java -jar app.jar , by calling one Rest function that must return one User object, I get this exception: 但是,当我生成.jar文件并使用命令java -jar app.jar执行应用程序时,通过调用必须返回一个User对象的一个​​Rest函数,我得到了以下异常:

2015-10-28 14:39:35.963  WARN 27836 --- [nio-7777-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: (was java.lang.NullPointerException) (through reference chain: com.pfc.soriano.wsdbmodel.entity.User["files"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.pfc.soriano.wsdbmodel.entity.User["files"])
2015-10-28 14:39:35.963  WARN 27836 --- [nio-7777-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Handler execution resulted in exception: Could not write content: (was java.lang.NullPointerException) (through reference chain: com.pfc.soriano.wsdbmodel.entity.User["files"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.pfc.soriano.wsdbmodel.entity.User["files"])

My question is: What is Netbeans doing different from command line java -jar, which makes it work fine? 我的问题是:Netbeans与命令行java -jar有什么不同,这使其可以正常工作?

Trying to find some documentation more, I found this: JPA Transient Annotation and JSON 尝试查找更多文档,我发现了这一点: JPA瞬态注释和JSON

Thanks to Damien, finally I have my project working fine: Main Application.java: 多亏了Damien,终于使我的项目工作正常:Main Application.java:

@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {

    /* Here we register the Hibernate4Module into an ObjectMapper, then set this custom-configured ObjectMapper
     * to the MessageConverter and return it to be added to the HttpMessageConverters of our application*/
    public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();

        ObjectMapper mapper = new ObjectMapper();
        Hibernate4Module hm = new Hibernate4Module();
        hm.disable(Hibernate4Module.Feature.USE_TRANSIENT_ANNOTATION);
        mapper.registerModule(hm);

        messageConverter.setObjectMapper(mapper);
        return messageConverter;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(jacksonMessageConverter());
        super.configureMessageConverters(converters);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

User entity Class: 用户实体类:

@Entity
@Table(name = "user")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Long id;
    @Transient
    Collection<String> files;

    public Long getId() {
        files = Utils.getImages("" + id, "src/main/webapp/user/");
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Collection<String> getFiles() {
        return files;
    }

    public void setFiles(Collection<String> files) {
        this.files = files;
    }
}

UserDAO interface: UserDAO界面:

@RepositoryRestResource(collectionResourceRel = "users", itemResourceRel = "users")
public interface UserDAO extends JpaRepository<User, Long> {

}

UserController: UserController:

@Controller
@RequestMapping(value = "user")
public class UserController {

    @Autowired
    UserDAO userDAO;

    @RequestMapping(value = "findById", method = RequestMethod.POST)
    @ResponseBody
    public User findById(@Param("id") Long id) {
        return userDAO.findOne(id);
    }
}

Javascript function: JavaScript函数:

function loadUser(id) {
    $.ajax({
        type: "POST",
        url: serviceBaseUrl + "user/findById",
        data: {id: id},
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        success: function (data, textStatus, jqXHR) {
            if(data) {
                alert(data.files);
                /* DO SOMETHING ELSE */
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(jqXHR.responseJSON.message);
        }
    });
}

The result is that javascript show me an alert with the file names that exists on the server. 结果是javascript向我显示了服务器上存在的文件名的警报。

暂无
暂无

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

相关问题 Spring Boot + Hibernate - 如何维护@Transient字段 - Spring Boot + Hibernate - How to maintain @Transient fields 使用JPA和Hibernate Web应用程序在Spring-Boot上配置多个数据库 - Configure Multiple Database on Spring-Boot with JPA and Hibernate Web Application 在 spring-boot jpa hibernate 中 >4<24 后连接到 Db - Connection to Db dies after >4<24 in spring-boot jpa hibernate Hibernate JPA @OneToOne双向在Spring-Boot中不起作用 - Hibernate JPA @OneToOne Bidirectional Not Working in Spring-Boot 春季启动JPA,使用LIKE和NULL字段查询 - Spring-boot JPA, query with LIKE and NULL fields 加载spring-boot和spring-data-jpa时,Hibernate无法加载JPA 2.1 Converter - Hibernate fails to load JPA 2.1 Converter when loaded with spring-boot and spring-data-jpa Spring-Boot和Hibernate问题 - Spring-Boot and Hibernate issue 无法为事务打开JPA EntityManager-通信链接失败(Spring-boot / jpa / hibernate /连接池) - Could not open JPA EntityManager for transaction - Communications link failure (Spring-boot/jpa/hibernate/connection pool) 如何使用spring-boot,spring-data-jpa和hibernate获得“无级联”的行为? - How do I get “no cascading”-behaviour using spring-boot, spring-data-jpa and hibernate? 无法使用 spring-boot、spring-data-jpa 和 hibernate 空间在 ZE4728Fdb044B249BCBADE9 上进行地理空间查询(内部) - Unable to make geospatial query (within) with spring-boot, spring-data-jpa and hibernate spatial on postgresql db
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM