Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Database \ Spring Data MongoDB combat     - CentOS 6.2 install git error Perl5 (Linux)

- linux firewall configuration (Linux)

- Construction LVM-based raw device Oracle10g Database on Oracle Linux 5.11 (Database)

- Shell script on the variables with double quotation marks grep small problem (Programming)

- Ubuntu upgrade to Linux Kernel 4.2.3 Kernel (Linux)

- How to remove the Linux memory Cache, Buffer and swap space (Linux)

- Binary search -Java achieve (Programming)

- How VirtualBox and VMware virtual machine conversion (Linux)

- Oracle archive log size than the size of the online journal of the much smaller (Database)

- Use Oracle 11g show spparameter command (Database)

- The FreeBSD zfs: failed with error 6 Error Resolution (Linux)

- To install Docker under CentOS7 (Linux)

- The signature can not be verified under Debian (Linux)

- After installing Ubuntu 15.04, to do a few things (Linux)

- Nginx Installation and Configuration (Server)

- To learn from scratch OpenWrt perfect tutorial (Linux)

- Fedora 22 install Adobe Flash Player (Linux)

- Use ARChon runtime environment to run Android apps on Ubuntu (Linux)

- C ++ inline functions (Programming)

- Raspberry Pi configuration wireless hotspot (Linux)

 
         
  Spring Data MongoDB combat
     
  Add Date : 2018-11-21      
         
         
         
  This article will show in detail how to access Spring Data MongoDB MongoDB database. MongoDB is an open source document type NoSQL database, MongoDB is one of the Spring Data Spring Data modules dedicated to accessing MongoDB database. Spring Data MongoDB module provides both a query based on the method name, and also provides a query-based annotation.

1, with the Spring Data MongoDB configuration and management

To install the MongoDB database, can be downloaded from here: https: //www.mongodb.org/downloads
The installation process will be omitted. After completing MongoDB installed and running, you can start the application development.
First, create a simple Maven project in Eclipse, manage and configure the pom.xml dependency Spring Data MongoDB project.
pom.xml

< Project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    < ModelVersion> 4.0.0 < / modelVersion>
    < GroupId> SpringDataMongoDBDemo < / groupId>
    < ArtifactId> SpringDataMongoDBDemo < / artifactId>
    < Version> 0.0.1-SNAPSHOT < / version>
    < Dependencies>
        < Dependency>
            < GroupId> org.springframework.data < / ​​groupId>
            < ArtifactId> spring-data-mongodb < / artifactId>
            < Version> 1.7.2.RELEASE < / version>
        < / Dependency>
    < / Dependencies>
< / Project>

Eclipse will download the JAR package and the dependencies to the classpath configuration items. Now the project has been completed dependencies imported, you can start writing the actual code.
You need to first create a persistent entity classes MongoDB database.


Person.java

package com.ch.jpa.entity;

import java.util.ArrayList;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

@Document (Collection = "person")
public class Person {

    @Id
    private Long personId;

    private String name;

    private int age;

    @DBRef (Db = "address")
    private List < Address > addresses = new ArrayList <> ();

    public Person () {
    }

    @PersistenceConstructor
    public Person (Long personId, String name, int age) {
        super ();
        this.personId = personId;
        this.name = name;
        this.age = age;
    }

    public Long getPersonId () {
        return personId;
    }

    public void setPersonId (Long personId) {
        this.personId = personId;
    }

    public String getName () {
        return name;
    }

    public void setName (String name) {
        this.name = name;
    }

    public int getAge () {
        return age;
    }

    public void setAge (int age) {
        this.age = age;
    }

    public List < Address > getAddresses () {
        return addresses;
    }

    public void setAddresses (List < Address > addresses) {
        this.addresses = addresses;
    }

    @Override
    public String toString () {
        return "Person [personId =" + personId + ", name =" + name + ", age =" + age + ", addresses =" + addresses + "]";
    }
}
Note @Document represents persistent data is to be a collection. If the collection name is not specified, then the default class name is used as a collective name for the entity type.
Note @Id represents annotated field is mapped to the collection _id columns. If the entity class is not used in this comment, then the default domain name id will be mapped to the collection _id columns. And the value of this field is generated automatically by MongoDB driver package, its value is not available in the POJO.
Note @DBRef used to refer to an existing entity classes in the current entity class. However, in the case of a relational database is different, if we save the current entity, it does not save the associated entity references. Persistence related entity references are separated.
Note @PersistenceConstructor for marking constructor entity created when retrieving data from the MongoDB database server.

Here is the Address entity class associated with:

Address.java

package com.ch.jpa.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.mapping.Document;

@Document (Collection = "address")
public class Address {

    @Id
    private long addressId;

    private String address;

    private String city;

    private String state;

    private long zipcode;

    public Address () {

        System.out.println ( "CAlling default cons");
    }

    @PersistenceConstructor
    public Address (long addressId, String address, String city, String state, long zipcode) {
        super ();
        this.addressId = addressId;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zipcode = zipcode;
    }

    public String getAddress () {
        return address;
    }

    public void setAddress (String address) {
        this.address = address;
    }

    public String getCity () {
        return city;
    }

    public void setCity (String city) {
        this.city = city;
    }

    public String getState () {
        return state;
    }

    public void setState (String state) {
        this.state = state;
    }

    public long getZipcode () {
        return zipcode;
    }

    public void setZipcode (long zipcode) {
        this.zipcode = zipcode;
    }

    @Override
    public String toString () {
        return "Address [address =" + address + ", city =" + city + ", state =" + state + ", zipcode =" + zipcode + "]";
    }
}
     
         
         
         
  More:      
 
- Oracle database online redo logs are several methods of recovery of deleted (Database)
- Comparison of one-time transaction and CTE insert data (Database)
- File easier to compare tools under Linux (Linux)
- MySQL master-slave delay problem (Database)
- Linux operating system security management skills (Linux)
- Limit the use of the request being Nginx Flood attack (Linux)
- Oracle through the alarm log view and inspect the main library, physical and snapshot standby database (Database)
- How to allow users to access only a specific database (MSSQL) (Database)
- Install Oracle 11g illustrations and dependent libraries under SUSE11 (Database)
- Linux file system structure Introduction (Linux)
- Ubuntu prevent arp attacks (Linux)
- Linux (SUSE) mount NTFS mobile hard practice (Linux)
- 3 ways to create a lightweight, durable system of Ubuntu Linux USB disk (Linux)
- redis configuration in detail (English) (Database)
- Create a DLL using MinGW and Attention (Programming)
- Shell for loop (Programming)
- Linux Basics Tutorial: Combining awk delete data before the specified date hdfs (Linux)
- Cobbler remotely install CentOS system (Linux)
- To batch create users under Linux (Linux)
- Java string equal size comparison (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.