Defining Relationships Between Service Entities - Liferay - Liferay Developer Network
As discuss earlier Liferay Service Builder create tables with badz.info But is there any way to provide Many To Many relationship between entities. Yes there is . As you can see 3 tables are created two for entities and one for mapping. [b]Liferay Portal EE + MySQL[/b] [b]How to map one-to-many or many-to- one relationship between entities in badz.info? [/b] I am. in the book and the same in Liferay's forums and blogs - in fact it is about how to use mapping relationship one-to-many with ServiceBuilder.
Click the Event entity and move your cursor over the Location entity. Liferay IDE draws a dashed line from the Event entity to the cursor. Click the Location entity to complete drawing the relationship. Liferay IDE turns the dashed line into a solid line, with an arrow pointing to the Location entity. Their relationship should show in Diagram mode and look similar to that of the figure below.
Switch to Source mode in the editor for your service. Defining Ordering of Service Entity Instances Often, you want to retrieve multiple instances of a given entity and list them in a particular order. Liferay lets you specify the default order of the entities in your service. Suppose you want to return Event entities in order by date, earliest to latest, and you want to return Location entities alphabetically by name. Switch back to Overview mode in the editor for your service. Then select the Order node under the Event entity node in the outline on the left side of the view.
The IDE displays a form for ordering the Event entity. Check the Specify ordering checkbox to show the form for specifying the ordering.
Learn and Understand: One to Many Relationship in liferay using service bulider
Create an order column by clicking the add icon a green plus sign to the right of the table. Enter date for the column name to use in ordering the Event entity.
Click the Browse icon to the right of the By field and choose the asc option. This orders the Event entity by ascending date.
To specify ordering for Location entity instances, follow similar steps but specify name as the column and asc as the select by value. The last thing to do is to define the finder methods for retrieving entity instances from the database. It's just a shallow representation of the object.
All collections are not serialized by default. Or said another way any one to many, and many to many relationships are not serizialized by default. Object references are serialized by default. That being many to one and one to one relationships will be serialized by default. This is consistent with other libraries like hibernate and JPA which will eagerly load these relationships by default.
But, let's say we wanted to include the phoneNumbers field in our output then we could do the following: Collections are serialized in full including the shallow copy of the objects they contain.
So in our example, Person has a List field called phoneNumbers, and that List contains Phone instances.
This means Flexjson will send a shallow copy of Person, the List of phoneNumbers, and a shallow copy of each Phone instance within the List. So the output might look like: If you wanted to send both phoneNumbers and addresses you can call include method twice, or you can include two parameters to the include method using Java's new vararg feature.
Personally I prefer the later since I think it makes it shorter and easier to read. But, it's your choice. It's an easy fix using the dot notation. Here is an example to include the zipcode with each Address instance.
It's also smart enough to know that the addresses reference has to be included in order to include zipcode so you don't have to specify it twice. You can use the dot notation to trace over your object graph very easily.
More on Includes There is an option that allows you to specify an outer object for collections. Using the rootName method you can specify the property of that outer object.
Here is an example: The rootName method is new as of Flexjson 2. Prior versions used an alternate version of serialize. The reason for this new method allowed for more versions of serialize without requiring too many overload method combinations.
The exclude method allows you to exclude certain fields from serilization. This can come in handy if you have special fields that you don't want sent to the client like passwords, or secret data that should remain on the server.Liferay Tutorial 15 :- How to Create Service Builder in Liferay 6.2
If you exclude a nested field it implies that the rest of the parent object is included. So if I say exclude "head.
Liferay Portal Systems Development
The retinalScan member of rightEye field will be excluded, but both rightEye and head field will be included. The reason is in order to exclude retinalScan field you have to include the rightEye member.
If you didn't then it doesn't change anything because retinalScan wasn't going to be included in the first place. Said another way it's only the last field that is excluded all other parent fields are included. Excluding Using Annotations Always having to do your excludes in this manner can be cumbersome especially if you always intend something to be excluded or included.
Flexjson provides a way to express this using annotations. The JSON annotation can be used to mark a field in the object as included by default. Annotations can be placed on the methods or fields. A good example of this might be the Address object that contains a reference to Zipcode object. Since Zipcode is an integral part of every Address in the US then it might make more sense for us to default this with the annotation. So in our Address object we could do the following: That way you keep your code more DRYand that prevents accidental security flaws.
Consider if we store the a hashed version of a password in our User object. This makes it easy to include or exclude several fields very easily. It's very common for people to want to remove the class attribute written to the stream. With wildcards it becomes very easy to accomplish this. Wildcards match any depth you specify. So it doesn't matter how deep your object path has gotten.
So if flexjson is serializing the field with path of "foo. Wildcards do not expand the depth to which you are serializing in most cases. They only operate in the depth you have specified. If you wanted foo. Every object has a class member because of Object. There are cases where serialization depth will be expanded. You have to be careful when doing specifying wildcards with recursive structures. That is the same as doing a deep serialization so be careful using it.
Order of Evaluation Order of evaluation has changed as of 1. What happens if I include a field then I exclude that same field later? What will Flexjson do? The answer is includes and excludes are evaluated in order they were added.
This is important with the introduction of wildcards. Say I want to serialize just two fields of an object. I can do the following: If I reversed the order of the include and exclude calls it would exclude all of the fields, and the username field wouldn't be included? That's because internally flexjson would have a list of expressions.
Think of it like a list where the order is the following: It starts the head of the list and stops once it finds the first match. Direct Serialization To a Stream For simplicity Flexjson allows you to work with String objects, but but it's more efficient to work directly with the stream we want to send the json data over. You can supply a java.
Writer as the destination for the json output. Now we can go directly to the stream bypassing the conversion to String. Here is a simple example: Deep Serialization In 1.
As with serialize method deepSerialize method will use includes, excludes, and annotations to figure out what you want sent. However, includes are generally redundant except in the case where you want override an excluding annotation on a field.
Deep serialization will not serialize cycles in your graph so if you have a bi directional relationship say parent has a child and child has a parent. The back reference from child to parent won't be included in the JSON output. This is the same for shallow serialization as well.
Liferay Gnosis: Interview Questions
Here is a quick example: Deep serialization can send a lot more data that you anticipated if you use it frivolously. This means the names of the getter methods or the field names are used. You can control this using the JSON annotation. With this core restructuring, Transformers have much more flexibility and power.
What is a transformer? Transformers are classes that are used to handle the actual transformation of Java objects into JSON. How to configure a transformer There are 3 ways you can configure a transformer: Specify the property name using the dot notation. Specify a class such that all instances of that class with use the Transform.
The String property identifiers will override the class identifiers. Any other Date objects would use the BasicDateTransformer. If you wanted to override the default StringTransformer that Flexjson uses for Strings you could write configure in the following manner: Flexjson comes with the following default Transformers: The value is written to JSON output.
Initializes the transformation process. Handles the traversal of the Java Objects being translated. Calls the correct Transformer for the object being translated. Hosts and outputs the resulting JSON Maintains state and information about the object s being transformed.
Provides common utility methods for tailoring the JSON output. As a side note you may notice that the Transformer extends AbstractTransformer. It is recommended that custom transformers extend the AbstractTransformer although it is not required.
An introduction to Liferay Service Builder
Feel free to take a look in the AbstractTransformer to see what it does. There isn't much to it. Transforming a Collection In this example we'll take a look at the IterableTransformer which transforms collections. Additional information on the current object context is also held in the TypeContext. This context is then made available to all downstream transformations via the getContext.
Custom Transformer using Inline functionality This example of the FlatDateTransformer found in the Flexjson unit tests is more complex than the last. What it illustrates is the flexibility that Transformers have when needing to meet various requirements.
Let's take some time to understand what the FlatDateTransformer does and then we'll discuss how it does it. In this particular case a java. Date is transformed into 3 separate properties on a JSON object. For example, if I had an object named Task with a Date property named doOn along side name and description this Transformer would render the date inline with the other properties when the JSON is generated.
You may also notice that each one was automatically prefixed with the Date properties' name of doOn. This brings me to the prefix property that can be passed into the FlatDateTransformer.
The prefix allows us to alter the generated property name to whatever we like when inlining. Lets look at our Task example again but this time we'll provide a prefix. In order to accomplish this the FlatDateTransformer extends the AbstractTransformer and overrides the isInline method. This allows us full control over the generated JSON instead of relying on the standard reflection to lookup the property name or key name and use it as the JSON property name.
As a result we need to handle every aspect of the JSON generation for this object now. Once the transform method is called on the FlatDateTransformer it first checks to see if a TypeContext exists. If a type context exists it checks to see if a property name exists on the TypeContext and sets it. Afterwards it checks to see if a prefix has been specified. If a prefix does not exist it is replaced with the propertyName. Otherwise prefix is left as is.