google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

@Embedded don't save classname #325

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version are you using? (Morphia/Driver/MongoDB)
Morphia version:0.99
Driver:2.6.3
MongoDB:v1.8.4-pre
Please include a stack trace below:

@Entity(value = "player_friend", noClassnameStored = true)
public class PlayerFriend extends BaseModel {
    @Id
    private String playerId;

    @Embedded
    private List<Friend> friends = new ArrayList<Friend>();

    @Embedded
    private List<FriendReq> friendReqs = new ArrayList<FriendReq>();
...

----------------------------------
@Test
    public void testInitFriend() throws Exception {
        playerFriend = new PlayerFriend("123");
        ArrayList<Friend> friends = new ArrayList<Friend>();
        for (int i = 1; i <= 5; i++) {
            Friend friend = new Friend(String.valueOf(i));
            friend.setSex(i % 2);
            friend.setName("friend" + i);
            friend.setLevel(i);
            friend.setIntimate(10 - i);
            friends.add(friend);
        }
        playerFriend.setFriends(friends);
        new PlayerFriendDao().save(playerFriend);
    }

mongo player_friend table:
{
  "_id" : "123",
  "friends" : [{
      "id" : "1",
      "sex" : 1,
      "level" : 1,
      "intimate" : 9,
      "currentIntimate" : 0,
      "cityCode" : 0
    }, {
      "id" : "2",
      "sex" : 0,
      "level" : 2,
      "intimate" : 8,
      "currentIntimate" : 0,
      "cityCode" : 0
    }, {
      "id" : "3",
      "sex" : 1,
      "level" : 3,
      "intimate" : 7,
      "currentIntimate" : 0,
      "cityCode" : 0
    }, {
      "id" : "4",
      "sex" : 0,
      "level" : 4,
      "intimate" : 6,
      "currentIntimate" : 0,
      "cityCode" : 0
    }, {
      "id" : "5",
      "sex" : 1,
      "level" : 5,
      "intimate" : 5,
      "currentIntimate" : 0,
      "cityCode" : 0
    }],
}

but update player_friend

public static void addFriendReq(final String pid, final String otherId, final 
boolean isRecv) {
        final PlayerFriend playerFriend = getPlayerFriend(pid);
        final FriendReq friendReq = new FriendReq(otherId, isRecv);
        List<FriendReq> friendReqs = playerFriend.getFriendReqs();

        UpdateOperations<PlayerFriend> ops = new PlayerFriendDao().getUpdateOperations(playerFriend, PlayerFriend.class);
        if (friendReqs.size() >= 50) {
            friendReqs.remove(0);
        }
        friendReqs.add(friendReq);
        ops.set("friendReqs", friendReqs);
        SQLQueue.inst.addExecute(pid, Constants.TYPE_FRIEND, Constants.ACTION_UPDATE, playerFriend, ops);
    }

mongodb player_friend talbe

{
  "_id" : "123",
  "friendReqs" : [{
      "className" : "com.ppsea.data.core.model.PlayerFriend$FriendReq",
      "id" : "13",
      "isRecv" : false
    }],
  "friends" : [{
      "id" : "1",
      "sex" : 1,
      "level" : 1,
      "intimate" : 9,
      "currentIntimate" : 0,
      "cityCode" : 0
    }, {
      "id" : "2",
      "sex" : 0,
      "level" : 2,
      "intimate" : 8,
      "currentIntimate" : 0,
      "cityCode" : 0
    }, {
      "id" : "3",
      "sex" : 1,
      "level" : 3,
      "intimate" : 7,
      "currentIntimate" : 0,
      "cityCode" : 0
    }, {
      "id" : "4",
      "sex" : 0,
      "level" : 4,
      "intimate" : 6,
      "currentIntimate" : 0,
      "cityCode" : 0
    }, {
      "id" : "5",
      "sex" : 1,
      "level" : 5,
      "intimate" : 5,
      "currentIntimate" : 0,
      "cityCode" : 0
    }],
  "level" : 0
}

this saved FriendReq's className,Not the desired result .

result better like 

{
  "_id" : "123",
  "friendReqs" : [{
      "id" : "13",
      "isRecv" : false
    }],
  "friends" : [{
      "id" : "1",
      "sex" : 1,
      "level" : 1,
      "intimate" : 9,
      "currentIntimate" : 0,
      "cityCode" : 0
    }, {
      "id" : "2",
      "sex" : 0,
      "level" : 2,
      "intimate" : 8,
      "currentIntimate" : 0,
      "cityCode" : 0
    }, {
      "id" : "3",
      "sex" : 1,
      "level" : 3,
      "intimate" : 7,
      "currentIntimate" : 0,
      "cityCode" : 0
    }, {
      "id" : "4",
      "sex" : 0,
      "level" : 4,
      "intimate" : 6,
      "currentIntimate" : 0,
      "cityCode" : 0
    }, {
      "id" : "5",
      "sex" : 1,
      "level" : 5,
      "intimate" : 5,
      "currentIntimate" : 0,
      "cityCode" : 0
    }],
  "level" : 0
}

Thanks!

Original issue reported on code.google.com by liuji...@gmail.com on 19 Sep 2011 at 10:21

GoogleCodeExporter commented 9 years ago
That was a bug fixed in the current trunk. I will marked this fixed in 0.99.1 
which I hope (and have been hoping) to get out soon.

It only happened with an update, and it didn't check the type correctly.

Original comment by scotthernandez on 19 Sep 2011 at 2:49

GoogleCodeExporter commented 9 years ago
Thanks!
but i use morphia-1.00-SNAPSHOT.jar question still exists.
what about the time release 0.99.1 ?

Original comment by liuji...@gmail.com on 19 Sep 2011 at 3:27

GoogleCodeExporter commented 9 years ago
This issue was updated by revision r1780.

Original comment by scotthernandez on 13 Nov 2011 at 2:48

GoogleCodeExporter commented 9 years ago
The test could not reproduce the issue with the current code (soon to be 0.99.1)

Original comment by scotthernandez on 13 Nov 2011 at 2:49