Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jpa在mysql中存储java对象?_Mysql_Json_Spring Boot_Jpa - Fatal编程技术网

如何使用jpa在mysql中存储java对象?

如何使用jpa在mysql中存储java对象?,mysql,json,spring-boot,jpa,Mysql,Json,Spring Boot,Jpa,我已经使用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

我已经使用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;
}


}
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();
}
}
package com.example.demo;
导入net.sf.json.*;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.ResponseBody;
导入com.google.gson.gson;
导入com.google.gson.GsonBuilder;
导入com.google.gson.JsonObject;
导入net.sf.json.JSONObject;
@Controller//这意味着该类是一个控制器
@RequestMapping(path=“/demo”)//这意味着URL以/demo开头
(在应用程序路径之后)
公共类主控制器{
@Autowired//这意味着获取名为userRepository的bean
//这是由Spring自动生成的,我们将使用它
处理数据
私有用户存储库用户存储库;
@GetMapping(path=“/add”)//仅映射GET请求
public@ResponseBody String addNewUser(@RequestBody String json){
//@ResponseBody表示返回的字符串是响应,而不是视图名称
//@RequestParam表示它是GET或POST请求中的参数
//JSONObject=JSONObject.fromObject(json);
Gson Gson=new GsonBuilder().create();
Employee=gson.fromJson(json,Employee.class);
userRepository.save(雇员);
return“使用JPA成功添加到数据库!”;
}
@GetMapping(path=“/all”)
public@ResponseBody Iterable getAllUsers(){
//这将返回用户的JSON或XML
返回userRepository.findAll();
}
}

尝试为实体类添加可序列化的实现。 即


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

为什么不使用
员工
地址
之间的关系?谢谢。但现在该怎么办呢?我应该取消对Address中@Entity的注释吗?是的,取消注释它,将创建名为“Address”的表,因为首先执行所有create table查询,然后更新查询。
@Entity
public class Employee implements Serializable{
}