我有一个现有对象,想使用 Java + POJO 编解码器在 MongoDB 中序列化。由于某种原因,驱动程序尝试创建枚举实例而不是使用 valueOF:
org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'phase'. Failed to decode 'value'. Cannot find a public constructor for 'SimplePhaseEnumType'.
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:192)
at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:168)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:122)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:126)
at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
Run Code Online (Sandbox Code Playgroud)
枚举:
public enum SimplePhaseEnumType {
PROPOSED("Proposed"),
INTERIM("Interim"),
MODIFIED("Modified"),
ASSIGNED("Assigned");
private final String value;
SimplePhaseEnumType(String v) {
value = v;
}
public String value() {
return value;
}
public static SimplePhaseEnumType fromValue(String v) {
for (SimplePhaseEnumType c: SimplePhaseEnumType.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw …Run Code Online (Sandbox Code Playgroud) 我有代码从 2 个表中检索 2 个对象(由 JooQ 生成的 POJO)的一条记录。
Record record = dsl.select()
.from(ISSUE)
.leftJoin(CLIENT).on(CLIENT.ID.eq(ISSUE.CLIENT_ID))
.where(ISSUE.ID.eq(id))
.fetchOne();
JIssue jIssue = record.into(JIssue.class);
JClientRecord jClient = record.into(JClientRecord.class);
Run Code Online (Sandbox Code Playgroud)
表 ISSUE 和表 CLIENT 都有 PK 字段“ID”。
问题是当映射到(POJO.class)时,具有相同名称的表字段未正确映射到 POJO。在上面的示例中,jIssue 采用 jClient 的 id。
当使用 TableRecords 而不是 POJO 的所有映射正确完成时,但您不能在生成的 DAO 中使用 TableRecord,它需要生成的 POJO。
我该如何解决这个问题,以便 jooq 可以正确映射到生成的 pojo 字段?
我有一个Event类,它使用构建器模式来设置字段,最后将字段添加到JSON对象中。
public class Event{
private EventProcessor eventProcessor = new EventProcessor();
private String userName;
private String userID;
public Event setUserName(String userName){
this.userName = userName;
return this;
}
public Event setUserID(String userID){
this.userID = userID;
return this;
}
public void toJson(){
JSONObject json = new JSONObject();
if(null != userName)
json.put("userName", userName);
if(null != userID)
json.put("userID", userID);
// need help to convert json to "event"
eventProcessor.addToQueue(event);
}
}
Run Code Online (Sandbox Code Playgroud)
事件处理器类
public class EventProcessor{
static{
EventQueue eventQueue = new EventQueue<Event>();
} …Run Code Online (Sandbox Code Playgroud) 我想了解当您已经拥有 POJO 对象(作为实体)时创建 DTO 对象有什么好处。
在我的项目中我有:
如果我查看 DTO 对象类(让我们称之为 MyObjDTO)和相同的类,但 POJO 方面(让我们称之为 MyObjPOJO),除了 MyObjPOJO 作为注释之外没有任何区别,因为它是一个 @Entity。
所以事实上,我在我的项目中得到了 2 个类,它们看起来相同(相同的属性,相同的方法),但用于不同的 puprose。
IMO,在这种情况下,DTO 类是无用的,并且会增加应用程序的复杂性,因为我对 DTO 类所做的所有操作都可以用 POJO 类来完成,而且,对于单一类型的对象,我必须维护至少 2 个类(DTO 和POJO),例如,如果我添加一个属性,我必须在两个类中添加该属性。
我不是专家,我对自己的想法提出疑问;你怎么看待这件事 ?
普通旧 Java 对象是否可以拥有处理业务逻辑的方法,而不仅仅是 getter/setter 方法?
我看到的答案可能很复杂,所以对此没有严格的定义吗?我知道 POJO 无法实现接口、扩展类并且不能使用注释。
我有一个返回3个结果的聚合
{ "serverUsed" : "/127.0.0.1:27017" , "result" : [ { "_id" : "luke" , "times" : 56} , { "_id" : "albert" , "times" : 28} , { "_id" : "matt" , "times" : 28}] , "ok" : 1.0}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试迭代结果时,代码进入无限循环(无法理解为什么!!)
AggregationOutput output = coll.aggregate( match1, unwind, match2, group, sort, limit);
Iterable<DBObject> list= output.results();
while(list.iterator().hasNext()){
String id = (String) list.iterator().next().get("_id");
int times = Integer.parseInt(list.iterator().next().get("times").toString());
System.out.println("ID IS "+id+" time: "+times);
}
Run Code Online (Sandbox Code Playgroud)
输出也重复第一个结果:
ID IS luke time: 56
ID IS luke time: 56 …Run Code Online (Sandbox Code Playgroud) 我有一个要求,我必须打印保存在数据库中的值.我有pojo对象,我将其传递到列表中,然后将整个列表保存在数据库中.请找到以下代码.
List dataList=new ArrayList();
Vehicles vehiclePojoObject=new Vehicles();
vehiclePojoObject.setName("Cycle");
vehiclePojoObject.setColor("Blue");
dataList.add(vehiclePojoObject);
Run Code Online (Sandbox Code Playgroud)
现在我必须打印vehiclePojoObject包含的值.如何才能实现.请指导.提前致谢.
注意:没有一个对象,但有多个对象存储在列表中.
我使用jsonschema2pojo-maven-plugin v0.4.7从JSON模式生成POJO类.示例模式如下:
"features": {
"title": "Feature",
"description": "Name and type of every feature in the model",
"type": "array",
"items": {
"properties": {
"columnName": {
"description": "Name of the table column",
"type": "string"
},
"featureName": {
"description": "Name of that column's feature for the pipeline",
"type": "string"
},
"type": {
"description": "Type of the feature",
"type": "string"
}
},
"required": ["columnName", "type"]
}
Run Code Online (Sandbox Code Playgroud)
得到的POJO类有点如下:
public class Feature {
/**
* Name of the table column
*
*/
@JsonProperty("columnName")
private …Run Code Online (Sandbox Code Playgroud) 由于GSON使用反射设置对象(pojo)的字段,如果json属性包含空格怎么办?我们可以解决它而不告诉服务器吗?
示例:
"node": {
"Description": "Traffic Accident",
"Date of Event": "02 September 2015",
"Location": "Naypyitaw›Tatkon",
"Type": "blah blah",
}
Run Code Online (Sandbox Code Playgroud)
您不能将该字段命名为java pojo中的"事件日期".
希望有人能帮助我.我有ArrayList一Invoice堂课.我想要得到的是过滤这个ArrayList并找到其中一个属性与a匹配的第一个元素regex.这个Invoice类看起来像这样:
public class Invoice {
private final SimpleStringProperty docNum;
private final SimpleStringProperty orderNum;
public Invoice{
this.docNum = new SimpleStringProperty();
this.orderNum = new SimpleStringProperty();
}
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
我正在使用它regex (\\D+)进行过滤,以便查找orderNum属性中是否存在任何不具有整数格式的值.所以基本上我正在使用这个流
Optional<Invoice> invoice = list
.stream()
.filter(line -> line.getOrderNum())
.matches("(\\D+)"))
.findFirst();
Run Code Online (Sandbox Code Playgroud)
但它不起作用.任何的想法?我一直在寻找,我发现如何使用pattern.asPredicate()像这样:
Pattern pattern = Pattern.compile("...");
List<String> matching = list.stream()
.filter(pattern.asPredicate())
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
随着List的Integer,String等等,但我还没有找到如何与做POJO.任何帮助都感激不尽.美好的一天