我正在尝试通过h2数据库进行spring安全登录,我有两个实体:Users和UserRoles。它们通过一对多连接进行连接。 运行我的应用程序后,会创建UserRoles表和JoinTable,但不会创建表用户。 用户实体:
email
UserRoles实体:
Entity
@Table(name = "USERS")
public class Users {
@Id
@Column(name = "username", nullable = false)
private String username;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "enabled", nullable = false, columnDefinition = "1")
private int enabled;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "USER_ROLE", joinColumns = {
@JoinColumn(name = "username")}, inverseJoinColumns = {
@JoinColumn(name = "user_role_id")})
private Set<UserRoles> userRoles = new HashSet<UserRoles>(0);
....
}
hibernate.config.xml:
@Entity
@Table(name = "USER_ROLES")
public class UserRoles {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_role_id")
private int user_role_id;
@Column(name = "role", nullable = false)
private String role;
...
}
答案 0 :(得分:1)
1.尝试更改表名。
2.您可以更改属性
public int ApplyUpdates(DataTable Table, string SelectTextForUpdate, string IdentityFieldName = "") Command, string SelectTextForUpdate = "")
{
int Result = -1;
if (_ConnectionString != null && _ConnectionString != "")
{
using (SqlConnection connection = new SqlConnection(_ConnectionString))
{
connection.Open();
SqlTransaction trans = connection.BeginTransaction();
using (SqlDataAdapter adapter = new SqlDataAdapter())
{
using (SqlCommand command = new SqlCommand())
{
using (SqlCommandBuilder builder = new SqlCommandBuilder())
{
adapter.SelectCommand = command;
adapter.SelectCommand.Connection = connection;
builder.DataAdapter = adapter;
// Make only fields with changed values appear in the update command
// !!!!!!!!!!!!!!!!!!!!!!!!! Somewhere in 2016 this stopped working and it always updates all values whatever the value of this property !!!!!!!!!!!!!!!!!!!!!!!!!
builder.SetAllValues = false;
// Make the where clause of the update statement to have only the primary field in it.
builder.ConflictOption = ConflictOption.OverwriteChanges;
adapter.SelectCommand.CommandText = SelectTextForUpdate;
adapter.SelectCommand.Transaction = trans;
adapter.UpdateCommand = builder.GetUpdateCommand(true).Clone();
adapter.DeleteCommand = builder.GetDeleteCommand(true).Clone();
// create new insertcommand with extra parameter for getting the stupid identity field
SqlCommand inserter = new SqlCommand();
inserter = builder.GetInsertCommand(true).Clone();
if (IdentityFieldName != "")
{
inserter.CommandText += " SET @ID = SCOPE_IDENTITY()";
SqlParameter param = new SqlParameter();
param.Direction = ParameterDirection.Output;
param.Size = 4;
param.DbType = DbType.Int32;
param.ParameterName = "@ID";
inserter.Parameters.Add(param);
}
//put custom insertcommand into our adapter
adapter.InsertCommand = inserter;
// now dispose the original CommandBuilder. The original builder is bound to our adapter and will overwrite the insertcommand
// just before the adapter.update command, and thus dump your new parameter...
// The only way to break this evil spell is to dispose the bad sorcerer
builder.Dispose();
// now create a temperary RowUpdated event, in this we can update the identity field of the table
adapter.RowUpdated += adapter_RowUpdated;
_Table = Table;
_IdentityFieldName = IdentityFieldName;
try
{
try
{
if (adapter.InsertCommand != null)
adapter.InsertCommand.Transaction = trans;
if (adapter.UpdateCommand != null)
adapter.UpdateCommand.Transaction = trans;
if (adapter.DeleteCommand != null)
adapter.DeleteCommand.Transaction = trans;
Result = adapter.Update(Table);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw new Exception(ex.Message);
}
}
finally
{
// get rid of the temperay RowUpdated event
adapter.RowUpdated -= adapter_RowUpdated;
_Table = null;
_IdentityFieldName = "";
inserter.Dispose();
}
Table.AcceptChanges();
}
}
}
}
}
在控制台中检查你的SQL查询
3.尝试更改属性<property name="show_sql">true</property>
希望这会有所帮助!!
答案 1 :(得分:0)
Hibernate不允许使用&#34;密码&#34;创建一个表。柱。重命名为passworT解决了这个问题。