私は大学コースの基本的なプロジェクトエディタを作ろうとしています。私はかなり前に休止状態を使用していないので、私はかなりの問題を抱えており、これは私に困惑しています。hibernate ElementCollectionが間違った列を探します
私は、プロジェクト内のアクティビティのリストをロードしようとすると、私はこのエラーを取得:
Hibernate: select activities0_.Project_Id as Project_6_0_0_, activities0_.activities_Id as activiti8_0_0_, activity1_.Id as Id1_0_1_, activity1_.Description as Descript2_0_1_, activity1_.Duration as Duration3_0_1_, activity1_.End_Date as End_Date4_0_1_, activity1_.Name as Name5_0_1_, activity1_.Project_Id as Project_6_0_1_, activity1_.Start_Date as Start_Da7_0_1_ from Activities activities0_ inner join Activities activity1_ on activities0_.activities_Id=activity1_.Id where activities0_.Project_Id=?
18:40:44.775 [qtp94264799-20] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1054, SQLState: 42S22
18:40:44.776 [qtp94264799-20] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unknown column 'activities0_.activities_Id' in 'field list'
適切に閉じていないセッションで、その結果、データがretreivedされていないと、プログラム全体として機能していません意図されました。
関与する2つのクラスがある:すべては進行中の非常に多くの仕事である
@Entity
@Table(name = "Projects")
public class Project implements java.io.Serializable{
private static final long serialVersionUID = 5115375190980452672L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Project_Id")
private int id; // this should be set once on creation and never touched
// again as it identifies the project uniquely
@Column(name = "Owner_Id")
private int ownerId;
@Column(name = "Name")
private String name;
@ElementCollection
@CollectionTable(name="Activities", [email protected](name="Project_Id"))
@Column(name="Activity")
private List<Activity> activities;
public Project(){
}
/**
* Constructor to load existing project
* @param idToLoad id of the project to load
*/
public Project(int idToLoad) {
// MySqlDriver.loadObjectWithId(this, idToLoad);
org.hibernate.Session session;
try{
session=Start.getFactory().getCurrentSession();
}catch(Exception e){
System.out.println("Could not load project.");
System.out.println("Error while creating mysql session: "+e);
return;
}
try{
session.beginTransaction();
session.load(this, idToLoad);
this.activities.size();
session.getTransaction().commit();
}catch(Exception e){
System.out.println("Could not load project.");
System.out.println("Error while committing changes: "+e);
session.getTransaction().commit();
}
}
/**
* Creates new project
* @param newName name of the new project
* @param newId id of the new project
*/
public Project(String newName, int owner_Id) { // on creation a project
// only has a name and a
// unique id
this.name = newName;
this.ownerId = owner_Id;
this.activities = null;
}
public void addActivity(String activityName) { // add new activity to the
// project with given
// attributes
Activity newActivity = new Activity(activityName, this.id);
this.activities.add(newActivity);
}
public void setName(String newName) { // change project name
this.name = newName;
}
public String getName() { // return project name
return this.name;
}
public List<Activity> getActivities(){
return this.activities;
}
public int getId(){
return this.id;
}
public void commitChanges() {
for (Activity act : this.activities)
act.commitChanges(); // commit changes made to all activities in the
// project
MySqlDriver.saveObject(this);
}
public void deleteProject() {
MySqlDriver.deleteObject(this);
}
public Activity selectActivity(String activityName) {
for (Activity act : activities) { // cycle through activities and return
// the one with the given name
if (act.getName().equals(activityName))
return act;
}
return null;
}
public void deleteActivity(String deleteMeName) {
for (Activity act : activities) { // cycle through activities and delete
// the one with the given name
if (act.getName().equals(deleteMeName)) {
MySqlDriver.deleteObject(act);
activities.remove(act);
break;
}
}
}
public void grantAccessToProject(String otherUser) {
// grants permission to view and modify project database to user
// otherUser
}
}
と
@Entity
@Table(name="Activities")
public class Activity {
private static final long minDateDiff = 3600000; //1H in milliseconds
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="Id")
private int id;
@Column(name="Name")
private String name; //using same name for activities in same project not supported.
@Column(name="Description")
private String description;
@Column(name="Start_Date")
@Temporal(TemporalType.TIMESTAMP)
private Date startDate;
@Column(name="End_Date")
@Temporal(TemporalType.TIMESTAMP)
private Date endDate;
@Column(name="Duration")
private long duration;
@Column(name="Project_Id")
private int parent;
/**
* Activity constructor, automatically sets default start and end date.
* @param newName Name of the new activity
* @param parent id of parent project
*/
public Activity(String newName, int parent){
this.parent = parent;
this.name = newName;
this.startDate = new Date(); //start date by default is creation time
this.endDate = new Date();
this.endDate.setTime(startDate.getTime() + Activity.minDateDiff); //end date by default is 1 hour ahead of start date
this.description = new String();
this.getDateDiff(startDate, endDate, TimeUnit.HOURS); //set default duration
}
public void getDateDiff(Date date1, Date date2, TimeUnit timeUnit) { //converts difference between dates from
//milliseconds to given time unit
//credit to Sebastien Lorber (from StackOverflow)
long diffInMillies = date2.getTime() - date1.getTime();
this.duration=timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS);
}
public void setName(String newName){
this.name = newName;
}
public String getName(){
return this.name;
}
public void setStartDate(Date newStartDate){
Date dummyDate = new Date();
if(newStartDate.getTime() > dummyDate.getTime()){ //rough check to ensure start date
//isn't in the past - precise enough
//for application's needs
this.startDate = newStartDate;
}
if(this.startDate.getTime() >= this.endDate.getTime()){ //shifts endDate 1 hour ahead of startDate if the new startDate
//is higher
this.endDate.setTime(this.startDate.getTime() + 3600000);
}
}
public void setEndDate(Date newEndDate){
Date dummyDate = new Date();
if(newEndDate.getTime() > dummyDate.getTime() && this.startDate.getTime() < newEndDate.getTime()){
//rough check to ensure end date
//isn't in the past or before start date - precise enough
//for application's needs
this.endDate = newEndDate;
}
}
public void setDescription(String newDescription){
this.description = newDescription;
}
public String getDescription(){
return this.description;
}
public void addManager(String newManager){
//this.activityManagers.add(newManager);
}
/**
* Returns how long the Activity will take.
* @return Activity duration in hours.
*/
public long getDuration(){ //returns how long the activity will take in hours.
return this.duration;
}
/**
* Saves Activity data to database
*/
public void commitChanges(){
MySqlDriver.saveObject(this);
}
public void loadData(int activityToLoadId){ //loads data of activity with name activityToLoadName from database
this.id = activityToLoadId;
MySqlDriver.loadObjectWithId(this, activityToLoadId);
}
}
そうな方法のいくつかはパッチワークですが、私はこの問題は、方法Iであると考えていますElementCollectionをマッピングします。
ご覧のとおり、「activities_Id」という列はそれほど魅力的ではなく、ロードしようとしているコードはsession.get()とhibernate.initialize() :
for(Integer projId : this.myProjectIds){
try{
Session newSession = Start.getFactory().getCurrentSession();
newSession.beginTransaction();
Project dummyProject = (Project) newSession.get(Project.class, projId);
Hibernate.initialize(dummyProject.getActivities());
this.myProjects.add(dummyProject);
newSession.getTransaction().commit();
}catch(Exception e){
System.out.println(e);
}
}
ご協力いただけると助かります。
-update- 私はOneToManyを使用してみましたが、今まで言及したことのない列についても同様の苦情があります。これは私が(代わりにelementcollectionの)プロジェクトクラスで、今持っているものです。
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Activity> activities;
、これは私が(代わりに親ID intの)活動クラスに持っているものです。
@ManyToOne
private Project parent;
それだけで列名を作ることのようですので
Hibernate: select activities0_.Project_Project_Id as Project_1_2_0_, activities0_.activities_Id as activiti2_2_0_, activity1_.Id as Id1_0_1_, activity1_.Description as Descript2_0_1_, activity1_.Duration as Duration3_0_1_, activity1_.End_Date as End_Date4_0_1_, activity1_.Name as Name5_0_1_, activity1_.parent_Project_Id as parent_P7_0_1_, activity1_.Start_Date as Start_Da6_0_1_, project2_.Project_Id as Project_1_1_2_, project2_.Name as Name2_1_2_, project2_.Owner_Id as Owner_Id3_1_2_ from Projects_Activities activities0_ inner join Activities activity1_ on activities0_.activities_Id=activity1_.Id left outer join Projects project2_ on activity1_.parent_Project_Id=project2_.Project_Id where activities0_.Project_Project_Id=?
15:54:02.463 [qtp94264799-20] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1146, SQLState: 42S02
15:54:02.464 [qtp94264799-20] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Table 'Project32.Projects_Activities' doesn't exist
:私は今、取得エラーがさらに混乱です。たぶん私はmysqlテーブルに何かが見当たりません、私は、 "Project_Id"のアクティビティの列と、プロジェクトテーブルの同じ名前のプライマリキーの間に参照があります。
ElementCollectionは何ではありませんを実装しますここで使用する必要があります。アクティビティはエンティティです。エンティティ間の関連付けには、OneToMany、OneToOne、ManyToOneまたはManyToManyを使用します。 ElementCollectionは、基本型または埋め込み可能な要素のコレクション用です。 –
私はそれを試みました(今質問を更新しました)、私はまだ似たような問題があります – Sauron
心配しないで、私は問題を見つけました、とにかく感謝 – Sauron