Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Database \ Spring Data MongoDB combat     - Linux shell script to adjust the Task Scheduler (Linux)

- RHEL6.5 install the latest version of Vim and increase support for the Python2.7.5 (Linux)

- Camouflage Nginx Web server version to prevent invasion (Linux)

- Oracle 11g RAC installation considerations Miscellany (Database)

- Through eight skills to let you become a super Linux end-user (Linux)

- Under Ubuntu 15.04 installation JDK8 (Linux)

- ActiveMQ5.11.1 and JDK version matching relation (Linux)

- You must ask yourself four questions before deploying Docker (Server)

- Spark parquet merge metadata problem (Server)

- On event processing browser compatibility notes (Programming)

- Shell scripts to copy all directories under the current directory of a certain type of file to the same directory (Linux)

- How to use the beta / unstable version of the software in Debian library (Linux)

- How to protect the Apache website under Linux system (Linux)

- Mind mapping software installed in CentOS 7 in XMind (Linux)

- Kali Linux virtualbox rc = Error 1908 workaround (Linux)

- Java Concurrency - processes and threads (Programming)

- PHP with FastCGI and mod_php Comments (Server)

- Android thread mechanism --AsyncTask (Programming)

- Oracle 11g How dataguard master repository to Oracle single instance data recovery (Database)

- HTML5 postMessage cross-domain data exchange (Programming)

 
         
  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:      
 
- Redhat 7 can only be read after installation Samba service catalog approach could not be written (Server)
- Talk Packages (Linux)
- To get Java class / jar package path (Programming)
- Everyone should know something about TCP (Linux)
- Linux system security settings (Linux)
- Linux --- file descriptors and redirection (Linux)
- The specified user to execute commands under Linux (Linux)
- Piostat - Monitoring and Statistics Linux process (Linux)
- Getting Started with Linux system to learn: how to check in a package is installed on Ubuntu (Linux)
- Java garbage collection and heap memory layout (Programming)
- Set multiple IP addresses for a single network card on Ubuntu 15.10 (Linux)
- Oracle 11g modify MEMORY_TARGET (Database)
- Oracle RMAN-06023 and ORA-19693 errors (Database)
- Live Wallpaper for Linux release adds dynamic background (Linux)
- Ubuntu 14.04 + xRDP + Xfce implement Windows Remote Desktop Connection (Linux)
- Linux beginners should know 12 commands (Linux)
- Executable file format Explanation under Linux (Linux)
- Editor of the popular Linux Gvim (Linux)
- installation and configuration of the PHP environment (Apache2) under Linux (Server)
- Oracle archive log deletion (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.