【问题标题】:How to store java objects in mysql using jpa?如何使用 jpa 在 mysql 中存储 java 对象?
【发布时间】:2019-10-30 14:58:47
【问题描述】:

我已使用 GSON 将 JSON 转换为 POJO。 我希望使用 JPA 的 save() 方法将 Employee 实体对象存储到 mysql 中。但我收到一条错误消息,提示“无法确定地址的类型”。那我该怎么办呢?

这是错误: 无法确定类型:地址

Employee.java

package com.example.demo;

import java.math.BigDecimal;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

import com.google.gson.annotations.Expose;

@Entity
public class Employee {

@Id
private int id;
private String name;
private int age;
private BigDecimal salary;
private String designation;
private Address address;
private long[] phoneNumbers;

/*Getter and Setter Methods*/
public int getId() {
    return id;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public BigDecimal getSalary() {
    return salary;
}

public void setSalary(BigDecimal salary) {
    this.salary = salary;
}

public String getDesignation() {
    return designation;
}

public void setDesignation(String designation) {
    this.designation = designation;
}


public long[] getPhoneNumbers() {
    return phoneNumbers;
}

public void setPhoneNumbers(long[] phoneNumbers) {
    this.phoneNumbers = phoneNumbers;
}

public Address getAddress() {
    return address;
}

public void setAddress(Address address) {
    this.address = address;
}


}

地址.java

package com.example.demo;

import javax.persistence.Entity;
import javax.persistence.Id;

//@Entity
public class Address {
@Id
private String street;
private String city;
private int zipCode;
public String getStreet() {
    return street;
}
public void setStreet(String street) {
    this.street = street;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public int getZipCode() {
    return zipCode;
}
public void setZipcode(int zipcode) {
    this.zipCode = zipcode;
}
@Override
public String toString(){
    return getStreet() + ", "+getCity()+", "+getZipCode();
}
}

控制器类

package com.example.demo;

import net.sf.json.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import net.sf.json.JSONObject;

@Controller    // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo 
(after Application path)
public class MainController {
@Autowired // This means to get the bean called userRepository
           // Which is auto-generated by Spring, we will use it 
to handle the data
private UserRepository userRepository;

@GetMapping(path="/add") // Map ONLY GET Requests
public @ResponseBody String addNewUser (@RequestBody String json) { 
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request


    //JSONObject jsonObject = JSONObject.fromObject(json);
    Gson gson=new GsonBuilder().create();
    Employee employee =gson.fromJson(json,Employee.class);
    userRepository.save(employee);
    return "Successfully added to database using JPA!";
    }

@GetMapping(path="/all")
public @ResponseBody Iterable<Employee> getAllUsers() {
    // This returns a JSON or XML with the users
    return userRepository.findAll();
}
}

【问题讨论】:

  • 为什么不使用EmployeeAddress之间的关系?

标签: mysql json spring-boot jpa


【解决方案1】:

尝试为您的实体类添加 implements Serializable。 即。

@Entity
public class Employee implements Serializable{
}

【讨论】:

    【解决方案2】:

    每当您在类中使用 @Entity 注释并尝试将其实例保存在数据库中时。最初,在休眠中形成了一个创建表命令,该命令在数据库中创建具有给定规范的表。与在员工表中一样,您可以指定所有数据成员的数据类型。当光标到达“地址”时,它给出了错误,因为休眠无法找到与此相关的任何数据类型或与此相关的任何表。 正如您在 Address 类中评论 @Entity 注释一样。因此,不会在数据库中创建与此相关的表。 地址是这个hibernate需要类(表)的类引用。由于类(表)不存在,所以错误来了。

    【讨论】:

    • 谢谢。但是现在应该怎么办?我应该取消注释地址中的@Entity 吗?
    • 是的,取消注释,名称为“address”的表将被创建,因为首先执行所有创建表查询然后更新查询。
    猜你喜欢
    • 2020-08-17
    • 2011-01-08
    • 1970-01-01
    • 2021-04-21
    • 2011-08-11
    • 2018-02-19
    • 2022-12-02
    • 2011-01-24
    相关资源
    最近更新 更多