无法使用Spring数据将嵌入式实体插入mongodb

时间:2018-05-11 00:40:15

标签: java mongodb spring-data embedded-documents

所以我想添加一些数据,但它

{
    "_id" : ObjectId("5a0c6748fb3aac66aafe26d7"),
    "picture" : "http://placehold.it/150x150",
    "name" : "Cablam",
    "email" : "leilaware@cablam.com",
    "city" : "Rabat",
    "location" : {
            "type" : "Point",
            "coordinates" : [
                    -6.75778,
                    33.97468
            ]
    }
}

现在当我尝试插入只是normale obejct它有效但当我尝试包含另一个对象的对象有一对一关系的嵌入式docuement它不起作用我发现该集合为空

用于弹簧配置它是XML

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <!-- <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> -->
    <constructor-arg name="mongo" ref="mongo"/>
    <constructor-arg name="databaseName" value="yourdb"/>
</bean>

<!-- Factory bean that creates the Mongo instance -->
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
  <property name="host" value="localhost"/>
</bean>

我要保存数据的类

@Repository
public class CustomerDAOImpl implements CustomerDAO {

@Autowired
private MongoTemplate mongoTemp ;

@Override
public List<Customer> getCustomers() {


     MongoOperations mongoOperation ; //= (MongoOperations) appContext.getBean("mongoTemplate");


    Location location = new Location(
            "point", 
            new ArrayList<Double>( Arrays.asList(11111111.1, 00000000.0))
            );

    Shop shop = new Shop("pic link", "superName","superEmail","supercity",location,true );

    // save
    mongoOperation = (MongoOperations) mongoTemp;

    mongoOperation.save(shop);

    //shopRepository.insert(shop);
    // now user object got the created id.
    System.out.println(">>>>>>>> >>> >> 1. use1r : " + shop);
  ....

模型/实体

@Document(collection="shops")
public class Shop {

@Id
private String id;
private String picture;
private String name;
private String email;
private String city;

//@GeoSpatialIndexed(type=GeoSpatialIndexType.GEO_2DSPHERE)
private Location location;

private Boolean isLiked;

/*private Date shownAfter;*/


public Shop(String picture, String name, String email, String city, Location location, Boolean isLiked/*, Date shownAfter*/) {
    super();
    this.picture = picture;
    this.name = name;
    this.email = email;
    this.city = city;
    this.location = location;
    this.isLiked = isLiked;
    //this.shownAfter = shownAfter;
}
...
}

无论如何都可以帮助我

0 个答案:

没有答案