no Classname Stored for @Embedded [moved]
This is Issue 293 moved from a Google Code project. Added by 2011-07-01T02:14:55.000Z by [email protected]. Please review that bug for more context and additional comments, but update this bug.
Original labels: Type-Defect, Priority-Low
Original description
What version are you using? (Morphia/Driver/MongoDB) 1.0 Snapshot /2.6.3 /1.8.2
<b>Please include a stack trace below:</b>
My testing codes:
@Test
public void addPhoneNumberUsingMorphia() {
// /DBCollection hotels = db.getCollection("hotels");
morphia.map(Hotel.class);
Query<Hotel> updateQuery = ds.createQuery(Hotel.class)
.field(Mapper.ID_KEY).equal("4e08019cedb93eb02c873b94");
UpdateOperations<Hotel> ops = ds.createUpdateOperations(Hotel.class)
.add("phoneNumbers",
new PhoneNumber(123, 456789, PhoneNumber.Type.PHONE));
ds.update(updateQuery, ops);
}
@Test
public void addPhoneNumberUsingMorphia1() {
// /DBCollection hotels = db.getCollection("hotels");
morphia.map(Hotel.class);
Query<Hotel> updateQuery = ds.createQuery(Hotel.class)
.field(Mapper.ID_KEY).equal("4e08019cedb93eb02c873b94");
Hotel h = ds.find(Hotel.class, Mapper.ID_KEY,
"4e08019cedb93eb02c873b94").get();
h.getPhoneNumbers().add(
new PhoneNumber(98, 7654321, PhoneNumber.Type.PHONE));
UpdateOperations<Hotel> ops = ds.createUpdateOperations(Hotel.class)
.set("phoneNumbers", h.getPhoneNumbers());
ds.update(updateQuery, ops);
}
Java Model:
.....
@Transient
private String temp;
@Embedded
private Address address;
@Embedded
private List<PhoneNumber> phoneNumbers;
...
Data In MongoDB
{
"className" : "com.google.code.morphia.testmodel.PhoneNumber",
"countryCode" : 98,
"localExtension" : 7654321,
"type" : "PHONE"
},
{
"countryCode" : 123,
"localExtension" : 456789,
"type" : "PHONE"
}
Reason:
The first method do not have className, but the second one have. The root cause is the way morphia to check is need classname or not, now the logic is encapsulated into a single method
public static boolean shouldSaveClassName(Object rawVal, Object convertedVal, MappedField mf) {
if (rawVal == null || mf == null)
return true;
if (mf.isSingleValue())
return !(mf.getType().equals(rawVal.getClass()) && !(convertedVal instanceof BasicDBList));
else
if ( convertedVal != null &&
convertedVal instanceof DBObject && (false: convertedVal is ArrayList)
!mf.getSubClass().isInterface() &&
!Modifier.isAbstract(mf.getSubClass().getModifiers()) &&
mf.getSubClass().equals(rawVal.getClass())) (false: mf.getSubClass() is PhoneNumber but rawVal.getClass() is ArrayList)
return false;
else
return true;
}
I do not know when convertedVal instanceof DBObject AND mf.getSubClass().equals(rawVal.getClass()) can be true, becuase mf is not SingleValue.
Hi I just encountered this problem. But there is something strange going on i don't know why I'll try to investigate more I will present two cases. When I save first one it does not save className in to embedded object "WebTemplate" but second case saves.
First case:
@Entity(noClassnameStored = true)
public class Customer implements IsSerializable {
public enum WebTemplateType {
CrewContract("CC");
private String text;
private WebTemplateType(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
@Embedded
public static class WebTemplate implements IsSerializable {
public String content;
public WebTemplate() {
}
public WebTemplate(String content) {
this.content = content;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
WebTemplate other = (WebTemplate) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
public HashMap<WebTemplateType, WebTemplate> templateMap = Maps.newHashMap();
}
Second case:
@Entity(noClassnameStored = true)
public class Customer implements IsSerializable {
public enum WebTemplateType {
CrewContract("İş Akti");
private String text;
private WebTemplateType(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
@Embedded
public static class WebTemplate implements IsSerializable {
public ObjectId id = new ObjectId();
public String templateName;
public String content;
public WebTemplate() {
}
public WebTemplate(String content) {
this.content = content;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
WebTemplate other = (WebTemplate) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
public HashMap<WebTemplateType, ArrayList<WebTemplate>> newTemplateMap = Maps.newHashMap();
}
Example mongodb entiries:
First case:
{
...
templateMap:{ "CrewContract" : { "content" : "test"}}
}
Second case:
{
...
{ "CrewContract" : [ { "className" : "s.model.Customer$WebTemplate" , "id" : { "$oid" : "5214a73ebcfcc817abb2ca4c"} , "templateName" : "crew contract" , "content" : "test"}]}
}
@ismetozalp i know this pretty old at this point but can you look at org.mongodb.morphia.issue325.TestEmbeddedClassname in master (or try the latest 0.111-SNAPSHOT) in maven/master and see if this is actually an issue, still? I'm not sure it is but I don't see where you've attached a test case that recreates the results you've show. I don't have anything on hand that does either.
I'm calling this fixed, then. We can file a new issue if necessary.