Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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
Java Play Framework-Ebean-没有为类注册ScalarType“;“类名”;_Java_Mysql_Orm_Persistence_Ebean - Fatal编程技术网

Java Play Framework-Ebean-没有为类注册ScalarType“;“类名”;

Java Play Framework-Ebean-没有为类注册ScalarType“;“类名”;,java,mysql,orm,persistence,ebean,Java,Mysql,Orm,Persistence,Ebean,我在MySQL数据库中构建了一些表,使用持久性API和Ebean将自定义Java对象表示为表。现在,我尝试通过在Ebean.find()查询我想要的对象时作为参数传递来恢复存储的条目,但是每次都会抛出javax.persistence.PersistenceException 让我解释一下层次结构和结构: 有一个机器类: @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name

我在MySQL数据库中构建了一些表,使用持久性API和Ebean将自定义Java对象表示为表。现在,我尝试通过在Ebean.find()查询我想要的对象时作为参数传递来恢复存储的条目,但是每次都会抛出
javax.persistence.PersistenceException


让我解释一下层次结构和结构:

有一个机器类:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "MACHINE_TYPE")
public class Machine extends Model
{
    @Id
    @Column(name = "id")
    private Long _machineID;

    @Column(name = "name", unique = true)
    private String _name;

    @Column(name = "info")
    private String _info;

    // rest of code
}
@Entity
@Table(name = "machine")
@DiscriminatorValue("Container")
public class Container extends Machine
{
@Column(name = "dockerID", unique = true)
   private String _dockerID;

   @ManyToOne
   @JoinColumn(name = "host", insertable = true, updatable = true)
   private Server _host;

   @Column(name = "currentSize")
   private String _currentSize;

   @Column(name = "additionalInfo")
   private String _additionalInfo;

   @Column(name = "last_login")
   private String _lastLogin;

   // rest of code
}
有两个子类。服务器类扩展机器如下:

@Entity
@Table(name = "machine")
@DiscriminatorValue("Server")
public class Server extends Machine
{
   @Column(name = "ip")
   private String _ip;

   @Column(name = "port")
   private Integer _port;

   @Column(name = "user_name")
   private String _userName;

   @Column(name = "password")
   private String _password;

   @Column(name = "clock")
   private Integer _clock;

   @Column(name = "ram")
   private Integer _ram;

   // rest of code
}
`machine` (
  `machine_type` varchar(31) NOT NULL,
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `info` mediumtext,
  `dockerID` varchar(255) DEFAULT NULL,
  `host` bigint(20) DEFAULT NULL,
  `currentSize` varchar(255) DEFAULT NULL,
  `additionalInfo` text,
  `last_login` varchar(255) DEFAULT NULL,
  `ip` varchar(255) DEFAULT NULL,
  `port` int(11) DEFAULT NULL,
  `user_name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `clock` int(11) DEFAULT NULL,
  `ram` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_machine_dockerID` (`dockerID`),
  KEY `ix_machine__host_4` (`host`),
  CONSTRAINT `fk_machine__host_4` FOREIGN KEY (`host`) REFERENCES `machine` (`id`)
)
@ManyToOne(targetEntity = Server.class) // explicitly specify the target class
@JoinColumn(name = "host", insertable = true, updatable = true)
private Server host;
容器类:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "MACHINE_TYPE")
public class Machine extends Model
{
    @Id
    @Column(name = "id")
    private Long _machineID;

    @Column(name = "name", unique = true)
    private String _name;

    @Column(name = "info")
    private String _info;

    // rest of code
}
@Entity
@Table(name = "machine")
@DiscriminatorValue("Container")
public class Container extends Machine
{
@Column(name = "dockerID", unique = true)
   private String _dockerID;

   @ManyToOne
   @JoinColumn(name = "host", insertable = true, updatable = true)
   private Server _host;

   @Column(name = "currentSize")
   private String _currentSize;

   @Column(name = "additionalInfo")
   private String _additionalInfo;

   @Column(name = "last_login")
   private String _lastLogin;

   // rest of code
}

这段代码在MySQL数据库中构建了一个machine表,如下所示:

@Entity
@Table(name = "machine")
@DiscriminatorValue("Server")
public class Server extends Machine
{
   @Column(name = "ip")
   private String _ip;

   @Column(name = "port")
   private Integer _port;

   @Column(name = "user_name")
   private String _userName;

   @Column(name = "password")
   private String _password;

   @Column(name = "clock")
   private Integer _clock;

   @Column(name = "ram")
   private Integer _ram;

   // rest of code
}
`machine` (
  `machine_type` varchar(31) NOT NULL,
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `info` mediumtext,
  `dockerID` varchar(255) DEFAULT NULL,
  `host` bigint(20) DEFAULT NULL,
  `currentSize` varchar(255) DEFAULT NULL,
  `additionalInfo` text,
  `last_login` varchar(255) DEFAULT NULL,
  `ip` varchar(255) DEFAULT NULL,
  `port` int(11) DEFAULT NULL,
  `user_name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `clock` int(11) DEFAULT NULL,
  `ram` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_machine_dockerID` (`dockerID`),
  KEY `ix_machine__host_4` (`host`),
  CONSTRAINT `fk_machine__host_4` FOREIGN KEY (`host`) REFERENCES `machine` (`id`)
)
@ManyToOne(targetEntity = Server.class) // explicitly specify the target class
@JoinColumn(name = "host", insertable = true, updatable = true)
private Server host;
这似乎是正确的。但是,如果我在数据库中添加一个条目并尝试执行

...
Server server = Server.getServerByName(serverName); // this returns a Server object correctly
List<Container> testCase = Ebean.find(Container.class)
                    .where()
                    .eq("host", server)
                    .findList(); // this throws the exception
即使我试图用

List<Container> testcase = Ebean.find(Container.class)
                    .where()
                    .eq("host", server.getMachineId()) // pass the serverID
                    .findList();


任何帮助,至少是关于遵循什么方向的帮助,都将不胜感激。因此,经过一些测试,我发现如果您想使用对象访问数据库表,您需要遵循对象映射中的一些规则,而我显然没有。在文档中我也找不到这个。这个解决方案是失败和尝试的结果


首先,变量的名称必须与列的名称相同!如果我将
Container.class
Server.class
变量更改为:

@ManyToOne
@JoinColumn(name = "host", insertable = true, updatable = true)
private Server host; // notice the name is the same as the column name
一切正常,我可以在查询中传递
Server.class
对象,如:

Ebean.find(Container.class)
      .where()
      .eq("host", server)
      .findList();
因此,要映射对象实体,列名必须与变量名完全相同,反之亦然


其次,据我在文档中所读,我们可以在
@ManyToOne
注释中指定目标实体,如下所示:

@Entity
@Table(name = "machine")
@DiscriminatorValue("Server")
public class Server extends Machine
{
   @Column(name = "ip")
   private String _ip;

   @Column(name = "port")
   private Integer _port;

   @Column(name = "user_name")
   private String _userName;

   @Column(name = "password")
   private String _password;

   @Column(name = "clock")
   private Integer _clock;

   @Column(name = "ram")
   private Integer _ram;

   // rest of code
}
`machine` (
  `machine_type` varchar(31) NOT NULL,
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `info` mediumtext,
  `dockerID` varchar(255) DEFAULT NULL,
  `host` bigint(20) DEFAULT NULL,
  `currentSize` varchar(255) DEFAULT NULL,
  `additionalInfo` text,
  `last_login` varchar(255) DEFAULT NULL,
  `ip` varchar(255) DEFAULT NULL,
  `port` int(11) DEFAULT NULL,
  `user_name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `clock` int(11) DEFAULT NULL,
  `ram` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_machine_dockerID` (`dockerID`),
  KEY `ix_machine__host_4` (`host`),
  CONSTRAINT `fk_machine__host_4` FOREIGN KEY (`host`) REFERENCES `machine` (`id`)
)
@ManyToOne(targetEntity = Server.class) // explicitly specify the target class
@JoinColumn(name = "host", insertable = true, updatable = true)
private Server host;

在我的例子中,这并不是主要问题,因为Java读取
host
字段的类类型并自动传递目标实体。但是这里最好是安全的。

因此,经过一些测试,我发现如果您想使用对象访问数据库表,您需要在对象映射中遵循一些规则,而我显然没有。在文档中我也找不到这个。这个解决方案是失败和尝试的结果


首先,变量的名称必须与列的名称相同!如果我将
Container.class
Server.class
变量更改为:

@ManyToOne
@JoinColumn(name = "host", insertable = true, updatable = true)
private Server host; // notice the name is the same as the column name
一切正常,我可以在查询中传递
Server.class
对象,如:

Ebean.find(Container.class)
      .where()
      .eq("host", server)
      .findList();
因此,要映射对象实体,列名必须与变量名完全相同,反之亦然


其次,据我在文档中所读,我们可以在
@ManyToOne
注释中指定目标实体,如下所示:

@Entity
@Table(name = "machine")
@DiscriminatorValue("Server")
public class Server extends Machine
{
   @Column(name = "ip")
   private String _ip;

   @Column(name = "port")
   private Integer _port;

   @Column(name = "user_name")
   private String _userName;

   @Column(name = "password")
   private String _password;

   @Column(name = "clock")
   private Integer _clock;

   @Column(name = "ram")
   private Integer _ram;

   // rest of code
}
`machine` (
  `machine_type` varchar(31) NOT NULL,
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `info` mediumtext,
  `dockerID` varchar(255) DEFAULT NULL,
  `host` bigint(20) DEFAULT NULL,
  `currentSize` varchar(255) DEFAULT NULL,
  `additionalInfo` text,
  `last_login` varchar(255) DEFAULT NULL,
  `ip` varchar(255) DEFAULT NULL,
  `port` int(11) DEFAULT NULL,
  `user_name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `clock` int(11) DEFAULT NULL,
  `ram` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_machine_dockerID` (`dockerID`),
  KEY `ix_machine__host_4` (`host`),
  CONSTRAINT `fk_machine__host_4` FOREIGN KEY (`host`) REFERENCES `machine` (`id`)
)
@ManyToOne(targetEntity = Server.class) // explicitly specify the target class
@JoinColumn(name = "host", insertable = true, updatable = true)
private Server host;
在我的例子中,这并不是主要问题,因为Java读取
host
字段的类类型并自动传递目标实体。但这里最好安全