简体   繁体   English

ActiveJDBC数据模型返回元数据而不是数据

[英]ActiveJDBC data model returning metadata instead of data

I'm using JavaLite ActiveJDBC to pull data from a local MySQL server. 我正在使用JavaLite ActiveJDBC从本地MySQL服务器提取数据。 Here is my simple RestController: 这是我简单的RestController:

@RequestMapping(value = "/blogs")
@ResponseBody
public Blog getAllBlogs( )
    throws SQLException {

    Base.open( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/rainydaymatt", "root", "" ) ;
    List<Blog> blogs = Blog.where( "postType = 'General'" ) ;
    return blogs.get( 0 ) ;

}

And here's my simple model, which extends the ActiveJDBC Model class: 这是我的简单模型,它扩展了ActiveJDBC Model类:

public class Blog
extends Model {

}

Now, here's the problem:when I navigate to the path handled by the controller, I get this output stream: 现在,这是问题所在:当我导航到控制器处理的路径时,我得到以下输出流:

{"frozen":false,"id":1,"valid":true,"new":false,"compositeKeys":null,"modified":false,"idName":"id","longId":1}

I can tell that this is metadata about the returned objects because the number of these clusters changes based on my parameters - ie, when I select all, there are four, when I use a parameter, I get the same number as meets the criteria, and only one when I pull the first. 我可以说这是有关返回对象的元数据,因为这些簇的数量根据我的参数而变化-即,当我全选时,有四个,当我使用参数时,我得到的数字与满足条件的数字相同,当我拉第一个时只有一个。 What am I doing wrong? 我究竟做错了什么? Interestingly, when I revert to an old-school DataSource and use the old Connection/PreparedStatement/ResultSet, I'm able to pull data just fine, so the problem can't be in my Tomcat's context.xml or in the path of the Base.open. 有趣的是,当我恢复到老式的DataSource并使用旧的Connection / PreparedStatement / ResultSet时,我能够很好地提取数据,因此问题不会出在Tomcat的context.xml或路径中。 Base.open。

ActiveJDBC models can't be dumped out as raw output streams. ActiveJDBC模型不能作为原始输出流转出。 You need to do something like this to narrow your selection down to one model, and then refer to its fields. 您需要执行类似的操作以将选择范围缩小到一个模型,然后参考其字段。

Base.open( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/rainydaymatt", "root", "" ) ;
List<Blog> blogs = Blog.where( "postType = 'General'" ) ;
Blog tempBlog = blogs.get( 0 ) ;
return (String)tempBlog.get( "postBody" ) ;

as you stated already, a model is not String, so dumping it into a stream is not the best idea. 正如您已经说过的,模型不是String,所以将其转储到流中并不是最好的主意。 Since you are writing a service, you probably need JSON, XML or some other form of a String. 由于您正在编写服务,因此可能需要JSON,XML或某种其他形式的String。 Your alternatives are: 您的替代方法是:

JSON: JSON:

public Blog getAllBlogs() throws SQLException {
    Base.open( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/rainydaymatt", "root", "" ) ;
    String json = Blog.where( "postType = 'General'" ).toJson(false); ;
    Base.close();
    return json ;
}

XML: XML:

public Blog getAllBlogs() throws SQLException {
    Base.open( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/rainydaymatt", "root", "" ) ;
    String xml = toXml(true, false);
    Base.close();
    return xml;
}

Maps: 地图:

public Blog getAllBlogs() throws SQLException {
    Base.open( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/rainydaymatt", "root", "" ) ;
    List<Map> maps = Blog.where( "postType = 'General'" ).toMaps() ;
    String output =  doWhatYouNeedToGenerateOutput(maps);
    Base.close();
    return output;
}

Additionally, I must say you are not closing the connection in your code, and generally opening connections inside methods like this is not the best idea. 另外,我必须说您没有关闭代码中的连接,并且通常不建议在这种方法内部打开连接。 Your code will be littered by connection open and close statements in each method. 每个方法中的连接打开和关闭语句都会使您的代码杂乱无章。 Best to use a Servlet filter to open and close connections before/ after specific URIs. 最好使用Servlet过滤器在特定URI之前/之后打开和关闭连接。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM