Monday, 31 October 2022

Introduction to Hibernate

Hibernate is an Object/Relational Mapping solution for Java applications. Object relational mapping is a technique to map the object data to a relational data representation and vice versa. For example, an object to a table row/record.

 

Hibernate is an ORM framework, that sits between your java application and a Relational database. Java application use Hibernate APIs to perform create, update, delete, and query etc., operations.

 


 

 

In this tutorial series, I used

a.   Hibernate with postgresql and H2 to demonstrate the examples.

b.   JDK 15

 

 

 

References

https://docs.jboss.org/hibernate/orm/6.0/quickstart/html_single/

https://docs.jboss.org/hibernate/orm/6.0/topical/html_single/

https://docs.jboss.org/hibernate/orm/6.0/userguide/html_single/Hibernate_User_Guide.html


Previous                                                    Next                                                    Home

Hibernate 6 tutorial

      Introduction to Hibernate
      Hibernate 6: H2 Hello world application
      Hibernate 6: postgresql hello world application
      Hibernate 6: Embedded and Embeddable example
      org.hibernate.InstantiationException: Unable to locate constructor for embeddable
      Hibernate: Override the column properties of embedded type
      Hibernate6: quick guide to value types
      Hibernate 6: @Column: Specify details about this column
       Hibernate: @Formula: model computed value as a virtual read-only column
      Hibernate: Handle enum values
            Hibernate: handle enum values using @Enumerated annotation
            Hibernate 6: handle enum with @Enumerated String attribute
            Hibernate 6: Handle enum Using AttributeConverter
      Hibernate 6: Boolean values handling
      How hibernate map integer types?
      Hibernate 6: BigInteger mapping
      Hibernate 6: Double and Float values mapping
      Hibernate 6: BigDecimal mapping
      Hibernate 6: How to handle string data
      Hibernate 6: How to print all the data in a table?
      Hibernate 6: map a String to NVARCHAR
      Hibernate 6: Map a string to NCLOB
      Hibernate 6: work with character arrays
      Hibernate 6: work with byte arrays
      Hibernate 6: map a byte array to BLOB
      Hibernate 6: Example of BLOB, CLOB and NCLOB types
      Hibernate 6: map class references to VARCHAR type
      Hibernate 6: Map currency reference to VARCHAR
      Hibernate 6 : Map locale references to VARCHAR
      Hibernate 6: UUID mapping
      Hibernate 6: How to generate UUIDs as primary keys?
      Hibernate 6: InetAddress mapping
      Hibernate 6: Map a property to JSON (jsonb)
      Hibernate 6: Map a property to XML
      Hibernate 6: working with entity identifiers
      Hibernate 6: auto generate identifiers
      Hibernate 6: Generate identifiers using SEQUENCE strategy
      Hibernate 6: Generate identifiers using TABLE strategy
      Hibernate 6: Composite identifiers
            Hibernate 6: model Composite identifiers using @EmbeddedId
            Hibernate 6: Embedded id can contain ManyToOne attributes
            Hibernate 6: Composite identifiers with @IdClass
            Hibernate 6: Composite identifiers with associations
      Hibernate 6: @ManyToOne: Many to one mapping example
      Hibernate 6: @OneToMany: One to Many association
      Hibernate 6: Unidirectional One to Many association example
      Hibernate 6: Bidirectional One to Many association
      Hibernate 6: @OneToOne: one to one association
      Hibernate 6: Unidirectional one to one association
      Hibernate 6: bidirectional one to one mapping
      Hibernate 6: @ManyToMany: many to many association
      Hibernate6: uni directional many to many assoiciation
      Hibernate 6: bidirectional many to many association
      Hibernate 6: customize join table, join column names
       Hibernate 6: How to map collections?
      Hibernate 6: map lists using ElementCollection
      Hibernate 6: Customize list index start position while mapping lists
      Hibernate 6: Map a set using @ElementCollection
      Hibernate 6: Working with bags
      Hibernate 6: Get hibernate dialect
Previous                                                    Next                                                    Home

Sunday, 30 October 2022

Atlas Client: detach or remove term from an entity

AtlasClientV2#disassociateTermFromEntities method is used to remove a term from an entity or entities.

 

Signature

public void disassociateTermFromEntities(String termGuid, List<AtlasRelatedObjectId> relatedObjectIds) throws AtlasServiceException

 


To disassociate a term from an entity, we need to supply a relationshipGuid. You can get the relationshipGuid from entity response.

 

{
   "referredEntities": {},
   "entity": {
      "typeName": "DemoType6",
      "attributes": {
         "owner": null,
         "replicatedTo": null,
         "userDescription": null,
         "replicatedFrom": null,
         "qualifiedName": "AttributeDefaultValue_DEMO1",
         "displayName": null,
         "name": "AttributeDefaultValue_DEMO1",
         "x": 123,
         "description": null,
         "y": 2
      },
      "relationshipAttributes": {
         "inputToProcesses": [],
         "pipeline": null,
         "schema": [],
         "model": null,
         "meanings": [
            {
               "guid": "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
               "typeName": "AtlasGlossaryTerm",
               "entityStatus": "ACTIVE",
               "displayText": "sensitive",
               "relationshipType": "AtlasGlossarySemanticAssignment",
               "relationshipGuid": "c02d605e-6165-448e-a77c-0df916781fb0",
               "relationshipStatus": "ACTIVE",
               "relationshipAttributes": {
                  "typeName": "AtlasGlossarySemanticAssignment",
                  "attributes": {
                     "expression": null,
                     "createdBy": null,
                     "steward": null,
                     "confidence": null,
                     "description": null,
                     "source": null,
                     "status": null
                  }
               }
            }
         ],
         "outputFromProcesses": []
      },
      "classifications": [
         {
            "typeName": "sensitive_data",
            "attributes": {
               "durationInDays": 8
            },
            "entityGuid": "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
            "entityStatus": "ACTIVE",
            "propagate": true,
            "validityPeriods": [],
            "removePropagationsOnEntityDelete": false
         }
      ],
      "labels": []
   }
}

  ‘relationshipAttributes.meanings’ is an array. In this case we need parse the array ‘relationshipAttributes.meanings’ and fetch the term details that we want to detach and use it while removing.


Let’s remove the term ‘sensitive’ from the above entity (entityId = "e63cb343-675e-4cde-9d49-ceb43fb1aacb").

 

Find the below working application.

 

atlas-application.properties

atlas.client.readTimeoutMSecs=30000
atlas.client.connectTimeoutMSecs=30000

 

JsonUtil.java

package com.sample.app.util;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonUtil {
	public static String marshal(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(obj);
	}

	public static <T> T unmarshal(Class<T> clazz, String json)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		return (T) mapper.readValue(json, clazz);
	}

	public static String prettyPrintJson(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		return mapper.writeValueAsString(obj);
	}
}

 

DetachTermFromAnEntity.java

package com.sample.app.glossary;

import java.util.Arrays;
import java.util.Map;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo;
import org.apache.atlas.model.instance.AtlasRelatedObjectId;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sample.app.util.JsonUtil;

/**
 * We need a relationshipGuid to disassociate a term from the entity.
 * 
 * How to get the relationshipGuid? relationshipGuid
 * 
 * @author Krishna
 *
 */
public class DetachTermFromAnEntity {
	private static AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
			new String[] { "admin", "admin" });

	public static void detachTermFromAnEntity(String term, String entityId)
			throws JsonProcessingException, AtlasServiceException {
		AtlasEntityWithExtInfo atlasEntityWithExtInfo = atlasClient.getEntityByGuid(entityId);

		Map<String, Object> relationShipAttributes = atlasEntityWithExtInfo.getEntity().getRelationshipAttributes();
		Object meanings = relationShipAttributes.get("meanings");

		String json = JsonUtil.marshal(meanings);

		ObjectMapper mapper = new ObjectMapper();
		JsonNode jsonNode = mapper.readTree(json);

		if (!jsonNode.isArray()) {
			System.exit(0);
		}

		String relationShipGuid = null;
		String termId = null;
		int noOfElements = jsonNode.size();
		for (int i = 0; i < noOfElements; i++) {
			JsonNode node = jsonNode.get(i);

			if (!"AtlasGlossaryTerm".equals(node.get("typeName").asText())) {
				continue;
			}

			if (!term.equals(node.get("displayText").asText())) {
				continue;
			}

			relationShipGuid = node.get("relationshipGuid").asText();
			termId = node.get("guid").asText();

			break;
		}

		AtlasRelatedObjectId atlasRelatedObjectId = new AtlasRelatedObjectId();
		atlasRelatedObjectId.setGuid(entityId);
		atlasRelatedObjectId.setRelationshipGuid(relationShipGuid);

		atlasClient.disassociateTermFromEntities(termId, Arrays.asList(atlasRelatedObjectId));
	}

	public static void main(String[] args) throws AtlasServiceException, JsonProcessingException {

		String termToRemove = "sensitive";

		String entityId = "e63cb343-675e-4cde-9d49-ceb43fb1aacb";

		detachTermFromAnEntity(termToRemove, entityId);

	}
}

 

After running above application, I can see that the term ‘sensitive’ is removed.

 


 


Previous                                                    Next                                                    Home

Thursday, 20 October 2022

Atlas Client: Assign term to entities

AtlasClientV2#assignTermToEntities method is used to assign term to entities.

 

Signature

public void assignTermToEntities(String termGuid, List<AtlasRelatedObjectId> relatedObjectIds) throws AtlasServiceException

 

Example

String termId = "cd2dbbc5-0440-477e-a6e6-175eb13e3d46";

String objectId = "4eea7a7e-c59b-46ee-b6e9-a998251dada2";
String typeName = "DemoType16";

AtlasRelatedObjectId atlasRelatedObjectId = new AtlasRelatedObjectId();
atlasRelatedObjectId.setGuid(objectId);
atlasRelatedObjectId.setTypeName(typeName);

List<AtlasRelatedObjectId> relatedObjectIds = Arrays.asList(atlasRelatedObjectId);

 


Find the below working application.

 

atlas-application.properties

atlas.client.readTimeoutMSecs=30000
atlas.client.connectTimeoutMSecs=30000

AssignTermToEntities.java

package com.sample.app.glossary;

import java.util.Arrays;
import java.util.List;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.instance.AtlasRelatedObjectId;

public class AssignTermToEntities {
	public static void main(String[] args) throws AtlasServiceException {
		AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
				new String[] { "admin", "admin" });

		String termId = "cd2dbbc5-0440-477e-a6e6-175eb13e3d46";

		String objectId = "4eea7a7e-c59b-46ee-b6e9-a998251dada2";
		String typeName = "DemoType16";

		AtlasRelatedObjectId atlasRelatedObjectId = new AtlasRelatedObjectId();
		atlasRelatedObjectId.setGuid(objectId);
		atlasRelatedObjectId.setTypeName(typeName);

		List<AtlasRelatedObjectId> relatedObjectIds = Arrays.asList(atlasRelatedObjectId);
		
		atlasClient.assignTermToEntities(termId, relatedObjectIds);
	}
}

You can observe that the term ‘sensitive’ is attached to the entity.



  

Previous                                                    Next                                                    Home

Wednesday, 19 October 2022

Hive: tan(n): Return tangent of a number in radians

Signature

tan(n)

hive> SELECT tan(90);
OK
_c0
-1.995200412208242
Time taken: 0.047 seconds, Fetched: 1 row(s)
hive> ;
hive> SELECT tan(45);
OK
_c0
1.6197751905438615
Time taken: 0.036 seconds, Fetched: 1 row(s)

 

 

 

 

 

Previous                                                    Next                                                    Home

Atlas Client: Get entities assigned with a term

AtlasClientV2#getEntitiesAssignedWithTerm method is used to get the entities attached with given term.

 

Signature

public List<AtlasRelatedObjectId> getEntitiesAssignedWithTerm(String termGuid, String sortByAttribute, int limit, int offset) throws AtlasServiceException

 

Example

List<AtlasRelatedObjectId> entitiesAssignedToThisTerm = atlasClient.getEntitiesAssignedWithTerm(termId, null, 10, 0);



Find the below working application.

 

atlas-application.properties

 

atlas.client.readTimeoutMSecs=30000
atlas.client.connectTimeoutMSecs=30000

JsonUtil.java

package com.sample.app.util;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonUtil {
	public static String marshal(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(obj);
	}

	public static <T> T unmarshal(Class<T> clazz, String json)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		return (T) mapper.readValue(json, clazz);
	}

	public static String prettyPrintJson(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		return mapper.writeValueAsString(obj);
	}
}

 

GetEntitiesAssignedWithGivenTerm.java

package com.sample.app.glossary;

import java.util.List;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.instance.AtlasRelatedObjectId;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sample.app.util.JsonUtil;

public class GetEntitiesAssignedWithGivenTerm {

  public static void main(String[] args) throws AtlasServiceException, JsonProcessingException {
    AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
        new String[] { "admin", "admin" });

    String termId = "cd2dbbc5-0440-477e-a6e6-175eb13e3d46";

    List<AtlasRelatedObjectId> entitiesAssignedToThisTerm = atlasClient.getEntitiesAssignedWithTerm(termId, null,
        10, 0);

    System.out.println(JsonUtil.prettyPrintJson(entitiesAssignedToThisTerm));
  }

}

 

Output

[ {
  "guid" : "95731109-eb31-44e9-896e-60652e119312",
  "typeName" : "DemoType7",
  "entityStatus" : "ACTIVE",
  "displayText" : "CreateEntities_DEMO1",
  "relationshipType" : "AtlasGlossarySemanticAssignment",
  "relationshipGuid" : "981b2432-c033-42f5-bf37-07e4c3e41fad",
  "relationshipStatus" : "ACTIVE",
  "relationshipAttributes" : {
    "typeName" : "AtlasGlossarySemanticAssignment",
    "attributes" : {
      "expression" : null,
      "createdBy" : null,
      "steward" : null,
      "confidence" : null,
      "description" : null,
      "source" : null,
      "status" : null
    }
  }
}, {
  "guid" : "e63cb343-675e-4cde-9d49-ceb43fb1aacb",
  "typeName" : "DemoType6",
  "entityStatus" : "ACTIVE",
  "displayText" : "AttributeDefaultValue_DEMO1",
  "relationshipType" : "AtlasGlossarySemanticAssignment",
  "relationshipGuid" : "c02d605e-6165-448e-a77c-0df916781fb0",
  "relationshipStatus" : "ACTIVE",
  "relationshipAttributes" : {
    "typeName" : "AtlasGlossarySemanticAssignment",
    "attributes" : {
      "expression" : null,
      "createdBy" : null,
      "steward" : null,
      "confidence" : null,
      "description" : null,
      "source" : null,
      "status" : null
    }
  }
} ]

 



Previous                                                    Next                                                    Home