Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH1452/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using NHibernate.Cfg;
using NHibernate.Criterion;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH1452
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void Configure(Configuration configuration)
{
base.Configure(configuration);
configuration.SetProperty(Environment.FormatSql, "false");
}

/// <summary>
/// push some data into the database
/// Really functions as a save test also
/// </summary>
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
session.Save(new Product
{
ProductId = "XO1234",
Id = 1,
Name = "Some product",
Description = "Very good"
});

session.Save(new Product
{
ProductId = "XO54321",
Id = 2,
Name = "Other product",
Description = "Very bad"
});

tran.Commit();
}
}

protected override void OnTearDown()
{
base.OnTearDown();

using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
session.Delete("from Product");
tran.Commit();
}
}

[Test]
public void Delete_single_record()
{
using (var session = OpenSession())
{
var product = new Product
{
ProductId = "XO1111",
Id = 3,
Name = "Test",
Description = "Test"
};

session.Save(product);

session.Flush();

session.Delete(product);
session.Flush();

session.Clear();

//try to query for this product
product = session.CreateCriteria(typeof (Product))
.Add(Restrictions.Eq("ProductId", "XO1111"))
.UniqueResult<Product>();

Assert.That(product, Is.Null);
}
}

[Test]
public void Query_records()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var product = session.CreateCriteria(typeof (Product))
.Add(Restrictions.Eq("ProductId", "XO1234"))
.UniqueResult<Product>();

Assert.That(product, Is.Not.Null);
Assert.That(product.Description, Is.EqualTo("Very good"));

var log = sqlLog.GetWholeLog();
//needs to be joining on the Id column not the productId
Assert.That(log.Contains("inner join ProductLocalized this_1_ on this_.Id=this_1_.Id"), Is.True);
}
}

[Test]
public void Update_record()
{
using (var session = OpenSession())
{
var product = session.CreateCriteria(typeof (Product))
.Add(Restrictions.Eq("ProductId", "XO1234"))
.UniqueResult<Product>();

Assert.That(product, Is.Not.Null);

product.Name = "TestValue";
product.Description = "TestValue";

session.Flush();
session.Clear();

//pull again
product = session.CreateCriteria(typeof (Product))
.Add(Restrictions.Eq("ProductId", "XO1234"))
.UniqueResult<Product>();

Assert.That(product, Is.Not.Null);
Assert.That(product.Name, Is.EqualTo("TestValue"));
Assert.That(product.Description, Is.EqualTo("TestValue"));
}
}
}
}
13 changes: 13 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH1452/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH1452">
<class name="Product" table="Products">
<id name="ProductId" column="ProductId" type="String" generator="assigned" />
<property name="Name" column="Name" />
<property name="Id" column="Id" unique="true"/>
<join table="ProductLocalized">
<key column="Id" property-ref="Id"/>
<property name="Description" column="Description"/>
</join>
</class>
</hibernate-mapping>
13 changes: 13 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH1452/Product.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace NHibernate.Test.NHSpecificTest.NH1452
{
public class Product
{
public virtual string ProductId { get; set; }

public virtual int Id { get; set; }

public virtual string Name { get; set; }

public virtual string Description { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,8 @@
<Compile Include="NHSpecificTest\NH3160\FixtureByCode.cs" />
<Compile Include="NHSpecificTest\NH3037\Entity.cs" />
<Compile Include="NHSpecificTest\NH3037\FixtureByCode.cs" />
<Compile Include="NHSpecificTest\NH1452\Fixture.cs" />
<Compile Include="NHSpecificTest\NH1452\Product.cs" />
<Compile Include="NHSpecificTest\NH2347\Entity.cs" />
<Compile Include="NHSpecificTest\NH2347\Fixture.cs" />
<Compile Include="NHSpecificTest\NH2664\Product.cs" />
Expand Down Expand Up @@ -3142,6 +3144,7 @@
<EmbeddedResource Include="NHSpecificTest\NH3139\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2812\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3141\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH1452\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2664\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2214\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2960\Mappings.hbm.xml" />
Expand Down
28 changes: 27 additions & 1 deletion src/NHibernate/Cfg/XmlHbmBinding/ClassBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,33 @@ private void BindJoin(HbmJoin joinMapping, Join join, IDictionary<string, MetaAt
log.InfoFormat("Mapping class join: {0} -> {1}", persistentClass.EntityName, join.Table.Name);

// KEY
SimpleValue key = new DependantValue(table, persistentClass.Identifier);
SimpleValue key;
if (!String.IsNullOrEmpty(joinMapping.key.propertyref))
{
string propertyRef = joinMapping.key.propertyref;
var propertyRefKey = new SimpleValue(persistentClass.Table)
{
IsAlternateUniqueKey = true
};
var property = persistentClass.GetProperty(propertyRef);
join.RefIdProperty = property;
//we only want one column
var column = (Column) property.ColumnIterator.First();
if (!column.Unique)
throw new MappingException(
string.Format(
"Property {0}, on class {1} must be marked as unique to be joined to with a property-ref.",
property.Name,
persistentClass.ClassName));
propertyRefKey.AddColumn(column);
propertyRefKey.TypeName = property.Type.Name;
key = new ReferenceDependantValue(table, propertyRefKey);
}
else
{
key = new DependantValue(table, persistentClass.Identifier);
}

key.ForeignKeyName = joinMapping.key.foreignkey;
join.Key = key;
key.IsCascadeDeleteEnabled = joinMapping.key.ondelete == HbmOndelete.Cascade;
Expand Down
3 changes: 3 additions & 0 deletions src/NHibernate/Mapping/Join.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public void AddProperty(Property prop)
prop.PersistentClass = PersistentClass;
}

//if we are joining to a non pk, this is the property of the class that serves as id
public Property RefIdProperty { get; set; }

public bool ContainsProperty(Property prop)
{
return properties.Contains(prop);
Expand Down
40 changes: 40 additions & 0 deletions src/NHibernate/Mapping/ReferenceDependantValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Mapping
{
/// <summary>
///
/// </summary>
[Serializable]
public class ReferenceDependantValue : DependantValue
{
private readonly SimpleValue _prototype;

public ReferenceDependantValue(Table table, SimpleValue prototype)
: base(table, prototype)
{
_prototype = prototype;
}

public IEnumerable<Column> ReferenceColumns
{
get { return _prototype.ConstraintColumns; }
}

public override void CreateForeignKeyOfEntity(string entityName)
{
if (!HasFormula && !string.Equals("none", ForeignKeyName, StringComparison.InvariantCultureIgnoreCase))
{
var referencedColumns = new List<Column>(_prototype.ColumnSpan);
foreach (Column column in _prototype.ColumnIterator)
{
referencedColumns.Add(column);
}

ForeignKey fk = Table.CreateForeignKey(ForeignKeyName, ConstraintColumns, entityName, referencedColumns);
fk.CascadeDeleteEnabled = IsCascadeDeleteEnabled;
}
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate/Mapping/SimpleValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public virtual bool IsComposite

#region IKeyValue Members

public void CreateForeignKeyOfEntity(string entityName)
public virtual void CreateForeignKeyOfEntity(string entityName)
{
if (!HasFormula && ! "none".Equals(ForeignKeyName, StringComparison.InvariantCultureIgnoreCase))
{
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/NHibernate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@
<Compile Include="Mapping\PrimaryKey.cs" />
<Compile Include="Mapping\PrimitiveArray.cs" />
<Compile Include="Mapping\Property.cs" />
<Compile Include="Mapping\ReferenceDependantValue.cs" />
<Compile Include="Mapping\RootClass.cs" />
<Compile Include="Mapping\Set.cs" />
<Compile Include="Mapping\SimpleValue.cs" />
Expand Down
Loading