如何在Java中创建实体

时间:2018-05-26 15:15:53

标签: java spring spring-data-jpa

我需要创建一个名为EventEvent的Entityan实体,它将包含多个字段,如namename,description description,owner owner等。我在这里搜索并添加了一些依赖项,但它不起作用。这是我的代码:

// Detects if device is on iOS 
const isIos = () => {
  const userAgent = window.navigator.userAgent.toLowerCase();
  return /iphone|ipad|ipod/.test( userAgent );
}
// Detects if device is in standalone mode
const isInStandaloneMode = () => ('standalone' in window.navigator) && (window.navigator.standalone);

// Checks if should display install popup notification:
if (isIos() && !isInStandaloneMode()) {
  this.setState({ showInstallMessage: true });
}

Here is an image of my folders and resources if helps to resolve the problem:

这是我的错误消息:

package com.example.app.entity;

import javax.persistence.*;
import java.util.List;

@Entity
@Table (name = "events")
public class Event {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

private String name;
private List<String> games;
private List<User> players;
private double latitude;
private double longitude;
private String description;
private User owner;

public User getOwner(){
    return owner;
}
public void setOwner(User owner){
    this.owner = owner;
}

public String getDescription(){
    return description;
}
public void setDescription(String description){
    this.description = description;
}

public double getLatitude(){
    return latitude;
}
public void setLatitude(double latitude){
    this.latitude = latitude;
}

public double getLongitude(){
    return longitude;
}
public void setLongitude( double longitude){
    this.longitude = longitude;
}

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

public long getId() {
    return id;
}

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

public List<String> getGames() {
    return games;
}

public void setGames(List<String> games) {
    this.games = games;
}

public List<User> getPlayers() {
    return players;
}

public void setPlayers(List<User> players) {
    this.players = players;
}

}

2 个答案:

答案 0 :(得分:0)

问题实际上是由the org.hibernate.MappingException: Could not determine type for: java.util.List引起的。所以,你需要放置Mapping注释(例如:OneToManyManyToMany等)

@OneToMany(targetEntity="your target enity", mappedBy="", fetch="Fetch Type")
private List<String> games;

或者基本实体(整数,字符串)

@Column
@ElementCollection(targetClass=String.class)
private List<String> games;

您需要为private List<User> players;

做同样的事情

有关详细信息,请访问link

答案 1 :(得分:0)

Hibernate无法映射对象列表。在表格形式中,它必须是另一个表的外键。 Hibernate可以使用延迟加载模式来初始化Collection。看到那篇文章 Hibernate, List<String>