I am trying to link two errors, but I am falling out
Here's a error:
mappedBy reference an unknown target entity property: com.proxy.ProxyBase.proxy_server in com.accounts.AllAccountsBase.proxies
First model
#Data
#Entity
#Table(name = "all_accounts")
public class AllAccountsBase {
#Id
#Column(name = "login", nullable = false, unique = true)
private String login;
#Column(name = "password")
private String password;
#Column(name = "old_password")
private String oldPassword;
#Column(name = "phone")
private String phone;
#Column(name = "recovery_email")
private String recoveryEmail;
#Column(name = "recovery_pass")
private String recoveryPass;
#Column(name = "virt_machine")
private String machineName;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "personal_data_id")
private PersonalData personalData;
#Column(name = "account_status")
private Integer accountStatus;
#Column(name = "time_update")
private Timestamp timeUpdate;
#Column(name = "server_group")
private String serverGroup;
#Column(name = "server", nullable = false, unique = true)
private String server;
#OneToMany(
mappedBy = "proxy",
cascade = CascadeType.ALL,
fetch = FetchType.LAZY
)
private Collection<ProxyBase> proxies = new ArrayList<>();
}
In the model, I added a relationship one to many, and vice versa in the other
Second model
#Data
#Entity
#Table(name = "proxy")
public class ProxyBase implements Serializable {
#Id
#Column(name = "server", nullable = false, unique = true)
private String server;
#Column(name = "proxy_data")
private String proxyData;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "all_accounts_server")
private AllAccountsBase allAccountsBase;
}
Help please
I did it differently:
#Data
#Entity
#Table(name = "proxy")
public class ProxyBase implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id")
private Long id;
#Column(name = "server", nullable = false)
private String server;
#Column(name = "proxy_data")
private String proxyData;
}
and
#Data
#Entity
#Table(name = "all_accounts")
public class AllAccountsBase implements Serializable {
#Id
#Column(name = "login", nullable = false, unique = true)
private String login;
#Column(name = "password")
private String password;
#Column(name = "old_password")
private String oldPassword;
#Column(name = "phone")
private String phone;
#Column(name = "recovery_email")
private String recoveryEmail;
#Column(name = "recovery_pass")
private String recoveryPass;
#Column(name = "virt_machine")
private String machineName;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "personal_data_id")
private PersonalData personalData;
#Column(name = "account_status")
private Integer accountStatus;
#Column(name = "time_update")
private Timestamp timeUpdate;
#Column(name = "server_group", nullable = false)
private String serverGroup;
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinTable(
name = "proxy_key",
joinColumns = #JoinColumn(name = "server_group", referencedColumnName = "server_group"),
inverseJoinColumns = #JoinColumn(name = "proxy_server_group", referencedColumnName = "server"))
private Collection<ProxyBase> proxies = new ArrayList<>();
}
Related
I am trying to get a list of unique objects where each object further contains a list of unique sub-objects. Uniqueness in below example is determined by the id field in each class.
public class MyMain {
public static void main(String[] args) {
Parent p1 = new Parent(1L);
Child c11 = new Child(11L);
Child c12 = new Child(12L);
Parent p2 = new Parent(2L);
Child c21 = new Child(21L);
Child c22 = new Child(22L);
Child c23 = new Child(23L);
Holder holder1 = new Holder(p1.getId(), c11.getId());
Holder holder2 = new Holder(p1.getId(), c11.getId());
Holder holder3 = new Holder(p1.getId(), c11.getId());
Holder holder4 = new Holder(p1.getId(), c11.getId());
Holder holder5 = new Holder(p1.getId(), c12.getId());
Holder holder6 = new Holder(p1.getId(), c12.getId());
Holder holder7 = new Holder(p1.getId(), c12.getId());
Holder holder8 = new Holder(p2.getId(), c21.getId());
Holder holder9 = new Holder(p2.getId(), c21.getId());
Holder holder10 = new Holder(p2.getId(), c21.getId());
Holder holder11 = new Holder(p2.getId(), c22.getId());
Holder holder12 = new Holder(p2.getId(), c23.getId());
Holder holder13 = new Holder(p2.getId(), c23.getId());
List<Holder> holders = new ArrayList<>();
holders.add(holder1); holders.add(holder2); holders.add(holder3); holders.add(holder4);
holders.add(holder5); holders.add(holder6); holders.add(holder7); holders.add(holder8);
holders.add(holder9); holders.add(holder10); holders.add(holder11); holders.add(holder12); holders.add(holder13);
}
}
#Value
#EqualsAndHashCode(onlyExplicitlyIncluded = true)
class Parent {
#EqualsAndHashCode.Include
public Long id;
public List<Child> chidren;
public Parent(Long id) { this.id = id; }
}
#Value
#EqualsAndHashCode(onlyExplicitlyIncluded = true)
class Child {
#EqualsAndHashCode.Include
public Long id;
public Child(Long id) { this.id = id; }
}
#Value
class Holder {
Long parentId;
Long childId;
public Holder(Long parentId, Long childId) {
this.parentId = parentId;
this.childId = childId;
}
}
From the code fragment above, I am looking to get a List<Parent> (from holders ) that will contain two parents: p1 and p2. Each parent will then have a List<Child> containing unique children for that parent.
Expected output:
List<Parent> will have p1 and p2
p1.List<Child> will have c11 and c12 (only 2 entries)
p2.List<Child> will have c21, c22, c23 (only 3 entries)
I have worked out how to get a list of unique parents but not sure how to achieve unique children as well.
UPDATE:
Below seems to be working for me, however, not sure if there's a better way.
public class MyMain {
public static void main(String[] args) {
MyMain m = new MyMain();
Parent p1 = new Parent(1L, null);
Child c11 = new Child(11L);
Child c12 = new Child(12L);
Parent p2 = new Parent(2L, null);
Child c21 = new Child(21L);
Child c22 = new Child(22L);
Child c23 = new Child(23L);
Holder holder1 = new Holder(p1.getId(), c11.getId());
Holder holder2 = new Holder(p1.getId(), c11.getId());
Holder holder3 = new Holder(p1.getId(), c11.getId());
Holder holder4 = new Holder(p1.getId(), c11.getId());
Holder holder5 = new Holder(p1.getId(), c12.getId());
Holder holder6 = new Holder(p1.getId(), c12.getId());
Holder holder7 = new Holder(p1.getId(), c12.getId());
Holder holder8 = new Holder(p2.getId(), c21.getId());
Holder holder9 = new Holder(p2.getId(), c21.getId());
Holder holder10 = new Holder(p2.getId(), c21.getId());
Holder holder11 = new Holder(p2.getId(), c22.getId());
Holder holder12 = new Holder(p2.getId(), c23.getId());
Holder holder13 = new Holder(p2.getId(), c23.getId());
List<Holder> holders = new ArrayList<>();
holders.add(holder1); holders.add(holder2); holders.add(holder3); holders.add(holder4);
holders.add(holder5); holders.add(holder6); holders.add(holder7); holders.add(holder8);
holders.add(holder9); holders.add(holder10); holders.add(holder11); holders.add(holder12); holders.add(holder13);
Map<Long, Set<Long>> returnSet= holders.stream()
.collect(Collectors.toMap(Holder::getParentId, x -> m.uniqChildIdSet(x), MyMain::merge));
System.out.println(returnSet);
}
public static Set<Long> uniqChildIdSet(Holder holder) {
HashSet<Long> uniqChild = new HashSet();
uniqChild.add(holder.getChildId());
return uniqChild;
}
public static Set<Long> merge(Set<Long> l1, Set<Long> l2) {
l1.addAll(l2);
return l1;
}
}
#Value
#EqualsAndHashCode(onlyExplicitlyIncluded = true)
class Parent {
#EqualsAndHashCode.Include
public Long id;
public List<Child> chidren;
public Parent(Long id, List<Child> chidren) { this.id = id;
this.chidren = chidren;
}
}
#Value
#EqualsAndHashCode(onlyExplicitlyIncluded = true)
class Child {
#EqualsAndHashCode.Include
public Long id;
public Child(Long id) { this.id = id; }
}
#Value
class Holder {
Long parentId;
Long childId;
public Holder(Long parentId, Long childId) {
this.parentId = parentId;
this.childId = childId;
}
}
Here is what you could use, without any helper methods.
What it's basically doing is the following
Group by the Holder's parent id
For this Holder, take all the childId properties and put them in a Set<Long>
Map<Long, Set<Long>> returnSet = holders.stream()
.collect(Collectors.groupingBy(
Holder::getParentId,
Collectors.mapping(
Holder::getChildId,
Collectors.toSet()
)
)
);
Output
{1=[11, 12], 2=[21, 22, 23]}
This Is my Json How can I Fetch this Fetch in Flutter? How Can I Create model For This ?
This Is Json Url http://api.weatherstack.com/current?access_key={MY API}&query=jaipur
you can use this project https://javiercbk.github.io/json_to_dart/ or this one https://app.quicktype.io/ and select Dart language to help you create models based on json data
You can automatically parse JSON to Dart classes by using this
class Weather {
Request request;
Location location;
Current current;
Weather({this.request, this.location, this.current});
Weather.fromJson(Map<String, dynamic> json) {
request =
json['request'] != null ? new Request.fromJson(json['request']) : null;
location = json['location'] != null
? new Location.fromJson(json['location'])
: null;
current =
json['current'] != null ? new Current.fromJson(json['current']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.request != null) {
data['request'] = this.request.toJson();
}
if (this.location != null) {
data['location'] = this.location.toJson();
}
if (this.current != null) {
data['current'] = this.current.toJson();
}
return data;
}
}
class Request {
String type;
String query;
String language;
String unit;
Request({this.type, this.query, this.language, this.unit});
Request.fromJson(Map<String, dynamic> json) {
type = json['type'];
query = json['query'];
language = json['language'];
unit = json['unit'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['type'] = this.type;
data['query'] = this.query;
data['language'] = this.language;
data['unit'] = this.unit;
return data;
}
}
class Location {
String name;
String country;
String region;
String lat;
String lon;
String timezoneId;
String localtime;
int localtimeEpoch;
String utcOffset;
Location(
{this.name,
this.country,
this.region,
this.lat,
this.lon,
this.timezoneId,
this.localtime,
this.localtimeEpoch,
this.utcOffset});
Location.fromJson(Map<String, dynamic> json) {
name = json['name'];
country = json['country'];
region = json['region'];
lat = json['lat'];
lon = json['lon'];
timezoneId = json['timezone_id'];
localtime = json['localtime'];
localtimeEpoch = json['localtime_epoch'];
utcOffset = json['utc_offset'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
data['country'] = this.country;
data['region'] = this.region;
data['lat'] = this.lat;
data['lon'] = this.lon;
data['timezone_id'] = this.timezoneId;
data['localtime'] = this.localtime;
data['localtime_epoch'] = this.localtimeEpoch;
data['utc_offset'] = this.utcOffset;
return data;
}
}
class Current {
String observationTime;
int temperature;
int weatherCode;
List<String> weatherIcons;
List<String> weatherDescriptions;
int windSpeed;
int windDegree;
String windDir;
int pressure;
double precip;
int humidity;
int cloudcover;
int feelslike;
int uvIndex;
int visibility;
String isDay;
Current(
{this.observationTime,
this.temperature,
this.weatherCode,
this.weatherIcons,
this.weatherDescriptions,
this.windSpeed,
this.windDegree,
this.windDir,
this.pressure,
this.precip,
this.humidity,
this.cloudcover,
this.feelslike,
this.uvIndex,
this.visibility,
this.isDay});
Current.fromJson(Map<String, dynamic> json) {
observationTime = json['observation_time'];
temperature = json['temperature'];
weatherCode = json['weather_code'];
weatherIcons = json['weather_icons'].cast<String>();
weatherDescriptions = json['weather_descriptions'].cast<String>();
windSpeed = json['wind_speed'];
windDegree = json['wind_degree'];
windDir = json['wind_dir'];
pressure = json['pressure'];
precip = json['precip'];
humidity = json['humidity'];
cloudcover = json['cloudcover'];
feelslike = json['feelslike'];
uvIndex = json['uv_index'];
visibility = json['visibility'];
isDay = json['is_day'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['observation_time'] = this.observationTime;
data['temperature'] = this.temperature;
data['weather_code'] = this.weatherCode;
data['weather_icons'] = this.weatherIcons;
data['weather_descriptions'] = this.weatherDescriptions;
data['wind_speed'] = this.windSpeed;
data['wind_degree'] = this.windDegree;
data['wind_dir'] = this.windDir;
data['pressure'] = this.pressure;
data['precip'] = this.precip;
data['humidity'] = this.humidity;
data['cloudcover'] = this.cloudcover;
data['feelslike'] = this.feelslike;
data['uv_index'] = this.uvIndex;
data['visibility'] = this.visibility;
data['is_day'] = this.isDay;
return data;
}
}
convert json to dart by Weather.fromJson(json);
I use hibernate orm to map have a many-to-many relationship between Role and Privilege like so:
Role
#Entity
#Audited
#JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "roleId")
public class Role extends BaseEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "role_id")
private Integer roleId;
#Size(max = 45)
#Column(name = "role")
private String role;
#ManyToMany(mappedBy = "roleCollection")
private Collection<Privilege> privilegeCollection;
#ManyToMany(mappedBy = "roleCollection")
#WhereJoinTable(clause = "privilege_privilege_id IN (SELECT ph.parent_privilege FROM privilege_hierachy ph)")
private Collection<Privilege> parentPrivilegeCollection;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "roleId")
#JsonIgnore
private Collection<User> userCollection;
public Role() {
}
//Getters and Setters removed for brevity
}
Privilege
#Entity
#Audited
#JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "privilegeId")
public class Privilege extends BaseEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "privilege_id")
private Integer privilegeId;
#Size(max = 45)
#Column(name = "name")
private String name;
#Size(max = 150)
#Column(name = "description")
private String description;
#Size(max = 45)
#Column(name = "friendly_name")
private String friendlyName;
#JoinTable(name = "role_privilege", joinColumns = {
#JoinColumn(name = "privilege_privilege_id", referencedColumnName = "privilege_id")}, inverseJoinColumns = {
#JoinColumn(name = "role_role_id", referencedColumnName = "role_id")})
#ManyToMany
private Collection<Role> roleCollection;
#JoinTable(name = "privilege_hierachy", joinColumns = {
#JoinColumn(name = "parent_privilege", referencedColumnName = "privilege_id")}, inverseJoinColumns = {
#JoinColumn(name = "child_privilege", referencedColumnName = "privilege_id")})
#ManyToMany
private Collection<Privilege> privilegeCollection;
public Privilege() {
}
// Getter and Setters removed for brevity
}
What i want to get is all Roles with their respective Privileges in JSON. Obviously that brings back StackOverflowError as you can see Role
goes to Privilege which goes back to Role and so on.
Because is many-to-many bidirectional i annotated it with #JsonIdentityInfo both sides of the relation.
The desired result is to get roles with privileges without cyclic stackoverflowerror, but what is get as result is Json below. What i dont understand is items in privilegeCollection some are objects which is fine but why numbers (57,97,165,161,124)... where are those comming from...why not objects
#RequestMapping(value = "/updateYearMaster", method = RequestMethod.POST)
public String updateYearmaster(#RequestParam(value = "id", required = false) Long id,
#RequestParam(value = "fromyear", required = false) Date fromyear,
#RequestParam(value = "toyear", required = false) Date toyear,
#RequestParam(value = "status", required = false) String status,
#RequestParam(value = "yeardescription", required = false) String yeardescription, Model model) {
Yearmaster yearmaster = new Yearmaster(fromyear, toyear, status, yeardescription);
yearmaster.setId(id);
List val = yearmasterService.duplicateEditYear(fromyear, toyear, id);
if (!val.isEmpty()) {
model.addAttribute("yearmaster", yearmaster);
errorMessage = "fromyear and toyear combination is already exist";
model.addAttribute("errorMessage", errorMessage);
return "edit-year-master";
} else {
yearmasterService.save(yearmaster);
return "redirect:/yearmaster";
}
}
I'm trying to get Microsoft.AspNet.Identity.EntityFramework": "3.0.0-beta6 to write some information to the current ApplicationUser "profile" but the value is null and I don't get any exceptions running the following code.
public class TestController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
public TestController(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
// GET: /<controller>/
public async Task<ActionResult> Index()
{
var userObject = await _userManager.FindByIdAsync(Context.User.GetUserId());
//This value is null even the second time I hit this breakpoint.
var shouldHaveAValue = userObject.MyStringList;
userObject.MyStringList = new List<string>();
userObject.MyStringList.Add("testId");
await _userManager.UpdateAsync(userObject);
return View();
}
}
I might be completely wrong here but this is the way I figured it should be done when I saw
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
public virtual List<string> MyStringList { get; set; }
}
The example is implemented in the ASP.NET5 web template.
//Edit:
Could it be the way I added the column? I did this since I get errors running the migrations commands which is stated not to be ready on the ef documentation page.( dnx . ef apply )
migration.CreateTable(
name: "AspNetUsers",
columns: table => new
{
Id = table.Column(type: "nvarchar(450)", nullable: false),
AccessFailedCount = table.Column(type: "int", nullable: false),
ConcurrencyStamp = table.Column(type: "nvarchar(max)", nullable: true),
Email = table.Column(type: "nvarchar(max)", nullable: true),
EmailConfirmed = table.Column(type: "bit", nullable: false),
LockoutEnabled = table.Column(type: "bit", nullable: false),
LockoutEnd = table.Column(type: "datetimeoffset", nullable: true),
NormalizedEmail = table.Column(type: "nvarchar(max)", nullable: true),
NormalizedUserName = table.Column(type: "nvarchar(max)", nullable: true),
PasswordHash = table.Column(type: "nvarchar(max)", nullable: true),
PhoneNumber = table.Column(type: "nvarchar(max)", nullable: true),
PhoneNumberConfirmed = table.Column(type: "bit", nullable: false),
SecurityStamp = table.Column(type: "nvarchar(max)", nullable: true),
TwoFactorEnabled = table.Column(type: "bit", nullable: false),
UserName = table.Column(type: "nvarchar(max)", nullable: true),
MyStringList = table.Column(type: "nvarchar(max)", nullable: true)
},