I'm currently trying to setup a database – using Java only. Given this simple class that might appear in the average social network app:
#Entity
class User {
#Id
private String email;
private String name;
private String otherInfo;
#ManyToMany
private List<User> contacts;
}
When the user logs in, he should receive the basic information and the list of contacts with their basic info, but not their contacts. To reduce the amount of boiler-plate code, I want to use a standard solution like Gson. However, even with lazy fetch the whole user is loaded on gson.toJson(user).
Therefore I thought of extracting the basic infos into a base class BasicUser and changing the contacts to List<BasicUser>. Now I only need to somehow circumwent the discriminator column when I fetch the contacts – of course they are all saved as complete users on the server. Unfortunately, I don't know how to archieve that. Any ideas?
If you need to get only part of the entity you can use projections. In your case it can be, for example, like this:
public interface BaseUser {
String getEmail();
String getName();
String getOtherInfo();
}
public interface UserRepo extends JpaRepository <User, String> {
List<BaseUser> findAllBy();
}
Using Jackson for serialization, the problem can be solved without writing custom serialization code. BasicUser contains the getters of the attributes, I want to serialize:
public interface BasicUser {
String getEmail();
String getFirstName();
String getLastName();
}
With a single annotation the contacts attribute is interpreted as a list of BasicUsers:
#Entity
public class User implements BasicUser {
#Id
private String email;
private String firstName;
private String lastName;
#ManyToMany
#JsonSerialize(contentAs = BasicUser.class)
private List<User> contacts = new ArrayList<>();
// ... implemented getters
}
You shouldn't have to modify your domain model just to accomodate a serialization library.
If you only want certain fields of a collection to be exposed to JSON, you could use Jackson with #JsonView (see here: How to serialize using #Jsonview with nested objects) not sure if Gson provides a similar feature as I have never used it extensively.
I have a class with a few members, and the associated setters and getters:
public class Tester implements Serializable {
#Column(name="ID", nullable=false, unique=true)
#Id
#GeneratedValue(generator="LOCATION_FACILITYTYPE_ID_GENERATOR")
#org.hibernate.annotations.GenericGenerator(name="LOCATION_FACILITYTYPE_ID_GENERATOR", strategy="native")
private int ID;
#Column(name="Value", nullable=false, unique=true, length=4)
private String value;
#Column(name="Name", nullable=false, unique=true, length=8)
private String name;
#ManyToOne(targetEntity=location.FacilityType.class, fetch=FetchType.LAZY)
#org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.LOCK})
#JoinColumns({ #JoinColumn(name="FacilityTypeID", referencedColumnName="ID", nullable=false) })
private location.FacilityType facility;
In a JUnit, I am trying to test creating a Tester element:
Tester trythis = new Tester();
trythis.setName("Herewe");
trythis.setValue("wow1");
Tester jnode = restTemplate.postForObject(TestBase.URL + "tester/", trythis, Tester.class);
This works as expected. However, if I use code like this to include an embedded member:
FacilityType ft = new FacilityType();
ft.setValue("AL");
ft.setName("2adamlec");
Tester trythis = new Tester();
trythis.setName("Herewe");
trythis.setValue("wow1");
trythis.setFacility(ft);
Tester jnode = restTemplate.postForObject(TestBase.URL + "tester/", trythis, Tester.class);
where the embedded member with value=AL does not yet appear in the database, I still get a new row created in the Tester table ... but the value and name columns in Tester are filled with the values (AL and 2adamlec) defined for FacilityType.
Note that we are using the JPARepository framework for FacilityType and Tester. The CRUD functions are thus handled 'under the covers', and I can't debug the POST processing. I wonder if this is associated with the fact that a GET for Tester data will only return the primitive fields in the JSON reply, since there is no projection defined for FacilityType.
Am I doing something wrong to cause the FacilityType fields to be saved in lieu of the desired Tester fields in the Tester table?
The short answer: when creating the item, you have to provide the data in the same JSON format that the server expects it. If you have an embedded class, you have to create a class where the facility member is a String to house a URL, then set that member to the URL corresponding to the existing instance of the embedded class. On the receiving end, you also need a new class like this:
public class FMT_Tester_RCV {
public FMT_Tester_RCV() { }
private String value;
private String name;
private Integer id;
private Integer ormid;
private JsonNode _links;
where you can travel down the JsonNode to get the link to the embedded class instance.
I've read other answers, but I still seem lost. I am trying to bring JSON into my objects. I call a JSON string through an API, and I want to convert it into the objects after the RequestManager. Am I missing methods or something? Also, the returnChampions2 function is wrong. I posted part of the JSON string after the methods, it's really long.
Public Class RequestManager
Public Function returnChampions2(ByVal strRegion As String) As LeagueChampionMaster
Dim strRequest As String = "https://global.api.pvp.net/api/lol/static-data/" + strRegion + "/v1.2/champion?api_key=" + _APIKey
Return JsonConvert.DeserializeObject(Of LeagueChampionMaster)(returnJSONRequest(strRequest))
End Function
Public Class LeagueChampionMaster
Private _Type As String
Private _Version As String
Private _Data As LeagueChampion()
End Class
Public Class LeagueChampion
Private _ID As Integer
Private _Key As String
Private _Name As String
Private _Title As String
End Class
The json:
{"type":"champion","version":"6.24.1","data":{"Jax":{"id":24,"key":"Jax","name":"Jax","title":"Grandmaster
at Arms"},"Sona":{"id":37,"key":"Sona","name":"Sona","title":"Maven of
the
Strings"},"Tristana":{"id":18,"key":"Tristana","name":"Tristana","title":"the
Yordle
Gunner"},"Varus":{"id":110,"key":"Varus","name":"Varus","title":"the
Arrow of
Retribution"},"Fiora":{"id":114,"key":"Fiora","name":"Fiora","title":"the
Grand
Duelist"},"Singed":{"id":27,"key":"Singed","name":"Singed","title":"the
Mad Chemist"},"TahmKench":{"id":223,"key":"TahmKench","name":"Tahm
Kench","title":"the River
King"},"Leblanc":{"id":7,"key":"Leblanc","name":"LeBlanc","title":"the
Deceiver"},"Thresh":{"id":412,"key":"Thresh","name":"Thresh","title":"the
Chain
Warden"},"Karma":{"id":43,"key":"Karma","name":"Karma","title":"the
Enlightened
One"},"Jhin":{"id":202,"key":"Jhin","name":"Jhin","title":"the
Virtuoso"},"Rumble":{"id":68,"key":"Rumble","name":"Rumble","title":"the
Mechanized
Menace"},"Udyr":{"id":77,"key":"Udyr","name":"Udyr","title":"the
Spirit Walker"},"LeeSin":{"id":64,"key":"LeeSin","name":"Lee
Sin","title":"the Blind
Monk"},"Yorick":{"id":83,"key":"Yorick","name":"Yorick","title":"Shepherd
of
Souls"},"Kassadin":{"id":38,"key":"Kassadin","name":"Kassadin","title":"the
Void
Walker"},"Sivir":{"id":15,"key":"Sivir","name":"Sivir","title":"the
Battle
Mistress"},"MissFortune":{"id":21,"key":"MissFortune","name":"Miss
Fortune","title":"the Bounty
Hunter"},"Draven":{"id":119,"key":"Draven","name":"Draven","title":"the
Glorious
Executioner"},"Yasuo":{"id":157,"key":"Yasuo","name":"Yasuo","title":"the
Unforgiven"},"Kayle":{"id":10,"key":"Kayle","name":"Kayle","title":"The
Judicator"},"Shaco":{"id":35,"key":"Shaco","name":"Shaco","title":"the
Demon
Jester"},"Renekton":{"id":58,"key":"Renekton","name":"Renekton","title":"the
Butcher of the
Sands"},"Hecarim":{"id":120,"key":"Hecarim","name":"Hecarim","title":"the
Shadow of
War"},"Fizz":{"id":105,"key":"Fizz","name":"Fizz","title":"the Tidal
Trickster"}}}
You have several issues. You should know that if you copy the json to the clipboard, Edit -> Paste Special -> Paste Json as Classes Visual Studio will create the classes for you to give you a decent starting point. In this case, the tool is a little dense and will create umpteen identical classes for "Fizz", "Shaco" etc. You have already normalized that.
However, with all the properties Private you wont be able to access the data. Then, the property names are wrong. The json key of id or key will not map/deserialize to _id or _key because they do not match. Lastly, the data collection should be a Dictionary. The key for each player/champion/item will be used as the Dictionary key:
Public Class LeagueContainer
Public Property type As String
Public Property version As String
Public Property data As Dictionary(Of String, DataItem)
End Class
Public Class DataItem
Public Property id As Integer
Public Property key As String
Public Property name As String
Public Property title As String
End Class
usage:
Dim jstr = ... from where ever ...
Dim myData = JsonConvert.DeserializeObject(Of LeagueContainer)(jstr)
' print the keys
For Each kvp In myData.data
Console.WriteLine(kvp.Key)
Next
' what is Yorick's title?
Console.WriteLine("Yorick is '{0}'", myData.data("Yorick").title)
(Partial) Output:
Jax
Sona
Tristana
Varus
Fiora
...
Yorick is 'Shepherd of Souls'
Hello I need to convert this objetct to vb.
{"user":
[
{"name":"CompanyName"},
{"password":"CompanyPassword"},
{"email":"mail#company.com"},
{"name":"UserName"},
{"email":"user#mail.com"}
]
}
I try with this:
Public Class InfoObjUser
Public Property name As String
Public Property password As String
Public Property email As String
End Class
Public Class ObjUser
Public Property user As New List(Of InfoObjUser)
End Class
but when I go to serialize json object created in vb
I see that there are no curly brackets.
Also in vb I can't add {"name":"UserName"},{"email":"user#mail.com"}
because they are already present.
Take a look at this: JSON Serialization in .Net.
You need to add the System.Runtime.Serialization as reference to your project, and then set up a serializer object in your code. You also need to tag, which fields in your class match your JSON code.
This would be your class setup:
<DataContract>
Public Class InfoObjUser
<DataMember(Name:="Name")>
Public Property name As String
<DataMember(Name:="Password")>
Public Property password As String
<DataMember(Name:="Email")>
Public Property email As String
End Class
<DataContract>
Public Class ObjUser
<DataMember>
Public Property user As New List(Of InfoObjUser)
End Class
And a Json reader class for that would look like this:
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Json
Public Class JsonReader
Public Shared Function FromString(Of T)(ByVal input As String) As T
Dim serializer As New DataContractJsonSerializer(GetType(T))
Dim memStream As New MemoryStream()
Dim sw As New StreamWriter(memStream)
sw.Write(input)
sw.Flush()
memStream.Position = 0
Dim returnObj As T = CType(serializer.ReadObject(memStream), T)
Return returnObj
End Function
End Class
And finally, you can call this class like this:
Dim jsonString ' = ...Input string here
Dim user As ObjUser = JsonReader.FromString(Of ObjUser)(jsonString)
However, it is not possible to have the name for two different data objects, as the serializer would not know where to map them to, as it goes by Object name.
I don't know how established this data structure already is, but if it's possible, I would suggest you to change the duplicated names, as no real mapping can occur when doing so programmatically.
<DataMember(Name:="UserName")>
Public Property username As String
<DataMember(Name:="UserEmail")>
Public Property usermail As String
If you would add something like this to your data model class and rename the field names in your Json, then it would work.
I wanted to create in JSF a combobox (selectOneMenu). I want to fill this combobox with all logins from one column from Database (SELECT logins FROM database).
Will be much gratefull from any help.
In your backing bean (YourBean in the example) you should have an array of Strings (or of objects with a getter method that returns the String you want). For instance lets assume you have the following code in your backing bean:
private ArrayList<String> logins; // read from DB
private String selectedLogin; // this will hold the selected value
// this method will be called by the JSF framework to get the list
public ArrayList<String> getLogins()
{
return logins;
}
public String getSelectedLogin()
{
return selectedLogin;
}
public String setSelectedLogin(String sl)
{
selectedLogin = sl;
}
In you Facelets page, assuming you are on JSF 2.x:
<h:selectOneMenu value="#{YourBean.selectedLogin}">
<f:selectItems value="#{YourBean.logins}" itemLabel="#{l}" itemValue="#{l}" var="l"/>
</h:selectOneMenu>
This will create a select menu with all the options in the array. Once the form is submitted the value will be set in the String value of your backing bean.