I have a two classes that are in OneToMany relationship:
Lager and supplier.
so when i have list of objects, i have also object and with standard java i can access fields of object.
when i do this
AbstractColumn columnName = ColumnBuilder.getNew()
.setColumnProperty("name", String.class.getName())
.setTitle("Name").setWidth(85)
.build();
i get a good result, but when i do
AbstractColumn sellerColumn = ColumnBuilder.getNew()
.setColumnProperty("seller" ,Seller.class.getName() )
.setTitle("seller").setWidth(85).build();
i just get toString method of my seller object, so my question is how to access field of seller when i am creating my DynamicJasper column
so i added now a picture to better
#OneToMany(mappedBy ="seller", cascade = CascadeType.REMOVE,fetch=FetchType.LAZY)
public List<Lager> getLager() {
return lager;
}
#ManyToOne(fetch=FetchType.EAGER)
#JoinColumn(name = "seller_id")
public Seller getSeller() {
return seller.get();
}
this are the relations between these two objects, how could i just show name of my seller object, and not the entire toString method.
Well at the end i found a way, i changed following method :
AbstractColumn sellerColumn = ColumnBuilder.getNew()
.setColumnProperty("seller" ,Seller.class.getName() )
.setTitle("seller").setWidth(85).build();
to
AbstractColumn sellerColumn = ColumnBuilder.getNew()
.setColumnProperty("seller.seller_name" ,Seller.class.getName() )
.setTitle("seller").setWidth(85).build();
after that everything worked, the thing is , i did try this, but for some reason my page did not load properly, when i did clean and build it worked flawlessly.
just to note that seller.seller_name is :
seller = my pojo class
and
seller_name
= my field that has getters and setters.
Related
I want to set many to many relationship oid key name.
In many to many relationship Oid is created automatically but on database side I want to change oid name to custom name.
For Example;
If I try to create Person and Task many to many relation. Third table attributes in below;
KomutTanim (FK to Makine)
Makine (FK to KomutTanim)
OID (PK, guid)** (I want to set this key name??)**
Tell me how can I do. I added sample code in below
[Association("Relation.KomutListesi_Makine",typeof(KomutTanim),UseAssociationNameAsIntermediateTableName = true),XafDisplayName("Makine Komutları")]
public XPCollection<KomutTanim> Komutlar
{
get
{
return GetCollection<KomutTanim>(nameof(Komutlar));
}
}
[Association("Relation.KomutListesi_Makine", typeof(Makine), UseAssociationNameAsIntermediateTableName = true), XafDisplayName("Makineler")]
public XPCollection<Makine> MasterId
{
get
{
return GetCollection<Makine>(nameof(MasterId));
}
}
You can customize XPO metadata or manually create a persistent class for your intermediate table. These approaches are illustrated in the How to implement a many-to-many relationship with an intermediate table ticket.
The solution with customizing XPO metadata uses XAF APIs to access an XPClassInfo instance via the XPDictionary property. You can access XPDictionary using only XPO methods as illustrated at How to get an XPClassInfo instance. Also, you can manually create a ReflectionDictionary instance (ReflectionDictionary is an XPDictionary descendant) as described in the How to create persistent metadata on the fly and load data from an arbitrary table article.
XPDictionary dictionary = new ReflectionDictionary();
XPClassInfo intermediateClassInfo = dictionary.GetClassInfo(typeof(KomutTanim)).FindMember(nameof(KomutTanim.MasterId)).IntermediateClass;
intermediateClassInfo.FindMember("Oid").AddAttribute(new PersistentAttribute("MyName"));
string conn = "My connection string";
IDataStore store = XpoDefault.GetConnectionProvider(conn, AutoCreateOption.SchemaAlreadyExists);
IDataLayer dl = new SimpleDataLayer(dictionary, store);
XpoDefault.DataLayer = dl;
In my .NET application, I recently had to make some changes in the database structure and upon changing code I have run into this error message.
The line used to say _categoryID = new Guid(Request.QueryString["CategoryID"].ToString()); which worked fine to retrieve a list of products based on the categoryid, but now I had to add a top level category called Market, and I used int instead of Guid in the database, because to me using Guid is a pain.
But now when I change the line I mentioned to _marketID = new Int32(Request.QueryString["MarketID"].ToString()); I get the error.
Here is the chunk of code :
#region Variables
Int32 _marketID;
#endregion
if ( Request.QueryString [ "MarketID" ] != null )
{
_marketID = new Int32(Request.QueryString["MarketID"].ToString());
ViewState["MarketID"] = _marketID;
BindDataToUI ( );
CreateFilterInSession ( );
}
Try this instead :
_marketID = Convert.ToInt32(Request.QueryString["MarketID"]);
note : no need to use ToString() for querystring values, they're all natively strings anyway.
Say for example if I have a business entity -> Customer, which has customerId, customerName and customerType. I have created an asp:Hidden Variable hdnCustomer to runat="server"
If I wanted to serialize the value of the customer business entity (in the code behind) to the hdnCustomer then how would I do that? Also once serialized how would I deserialize it?
// Pseudo code
Collection<Customer> customerList = new Collection<Customer>();
customerList = BusinessAccess.GetCustomerList();
hdnCustomer = serialize and assign the value of 'customerList' to hdnCustomer;
...
...
// Later on a select index change of one of the drop down lists
Inside the event handler for the drop down list:
{
Collection<Customer> customerList = new Collection<Customer>();
customerList = deserialize the value from hdnCustomer
int a = Convert.ToInt32(ddlDropDown.SelectedValue);
foreach(a in customerList)
{
// Do something
}
}
You can serialise to and from XML using XmlSerializer:
http://support.microsoft.com/kb/815813
However, if you just store the object in the ViewState[] collection that should work better:
ViewState["Customer"] = customerList;
It does the same thing: store the serialisable object in the page, hidden from the user: but it won't be in a human-readable format.
(edit: To deserialise, just get the value of ViewState["Customer"], checking for a null before using it!)
edit 2: a useful link about storing objects in ViewState:
http://www.beansoftware.com/ASP.NET-Tutorials/ViewState-In-ASP.NET.aspx
Hope that helps.
I think .net has already providing some classes to do so, look at this example
My problem is that I am trying to return a simple query that contains an object Story. The Story object has a UserId in the table which links to aspnet_users' UserId column. I have created a partial class for Story that adds the UserName property since it does not exist in the table itself.
The following query gets all stories; however, a pagination helper takes the query and returns only what's necessary once this is passed back to the controller.
public IQueryable<Story> FindAllStories(){
var stories = (from s in db.Stories
orderby s.DateEntered descending
select new Story
{
Title = s.Title,
StoryContent = s.StoryContent,
DateEntered = s.DateEntered,
DateUpdated = s.DateUpdated,
UserName = s.aspnet_User.UserName
}
);
return stories;
}
When the helper does a .count() on the source it bombs with the following exception:
"Explicit construction of entity type 'MyWebsite.Models.Story' in query is not allowed."
Any ideas? It's not a problem with the helper because I had this working when I simply had the UserName inside the Story table. And on a side note - any book recommendations for getting up to speed on LINQ to SQL? It's really kicking my butt. Thanks.
The problem is precisely what it tells you: you're not allowed to use new Story as the result of your query. Use an anonymous type instead (by omitting Story after new). If you still want Story, you can remap it later in LINQ to Objects:
var stories = from s in db.Stories
orderby s.DateEntered descending
select new
{
Title = s.Title,
StoryContent = s.StoryContent,
DateEntered = s.DateEntered,
DateUpdated = s.DateUpdated,
UserName = s.aspnet_User.UserName
};
stories = from s in stories.AsEnumerable() // L2O
select new Story
{
Title = s.Title,
StoryContent = s.StoryContent,
...
};
If you really need to return an IQueryable from your method and still need the Username of the user you can use DataContext.LoadOptions to eagerload your aspnet_user objects.
See this example.
i was looking at an example of how to do an insert in Linq to SQL and here it was it said:
NorthwindDataContext context = new NorthwindDataContext();
context.Products.Add(new Product(..));
context.SubmitChanges();
but when i look at the below, (in my case the Table is UserInfo), the Table doesn't have an "Add" method:
public System.Data.Linq.Table<UserInfo> UserInfos
{
get
{
return this.GetTable<UserInfo>();
}
}
any clue what i am doing wrong here?
You should use the InsertOnSubmit method:
NorthwindDataContext context = new NorthwindDataContext();
context.Products.InsertOnSubmit(new Product(..));
context.SubmitChanges();
The Add method exist on the EntitySet members, is mostly used when adding Child entities to a Parent one, for example:
var category = new Category{ Name = "Breveages"};
category.Products.Add(new Product{ Name = "Orange Juice"});
category.Products.Add(new Product{ Name = "Tomato Juice"});
category.Products.Add(new Product{ Name = "Cola"});
//...
context.Categories.InsertOnSubmit(category);
// This will insert the Category and
// the three Products we associated to.
EDIT: To do update operations, you just need to retrieve the entity by doing a query, or attaching it, for example:
var customer = context.Customers.Single( c => c.CustomerID == "ALFKI");
customer.ContactName = "New Contact Name";
context.SubmitChanges();
The DataContext tracks the changes of its related entities and when the SubmitChanges method is called, it will detect that change, and generate an Update SQL statement behind the scenes to do the update operation...