2016-10-18 4 views
0

テーブルにデータを挿入することはありません外部キーユーザを持つタスクOR32iteで1対多数のrealtionshipを持つテーブルにデータを挿入しないでください

これはタスククラスです:

package com.example.gencode.execomtodolist.model; 

import com.j256.ormlite.field.DatabaseField; 
import com.j256.ormlite.table.DatabaseTable; 


@DatabaseTable 
public class Task{ 

@DatabaseField(generatedId = true) 
private Long id; 

@DatabaseField 
private String title; 

@DatabaseField 
private String description; 

@DatabaseField 
private boolean done; 

@DatabaseField (foreign = true, foreignAutoRefresh = true) 
private User user; 

public Task(){ 

} 

public Task(User user, String title, String description, boolean done) { 
    this.user = user; 
    this.title = title; 
    this.description = description; 
    this.done = done; 
} 

@Override 
public String toString() { 
    return " " + title; 
} 

public Long getId() { 
    return id; 
} 

public String getTitle() { 
    return title; 
} 

public String getDescription() { 
    return description; 
} 

public User getUser() { return user; } 

public void setUser(User user) { this.user = user; } 

public boolean isDone() { return done; } 
} 

そして、これがユーザークラスです:

package com.example.gencode.execomtodolist.model; 

import com.j256.ormlite.dao.ForeignCollection; 
import com.j256.ormlite.field.DatabaseField; 
import com.j256.ormlite.field.ForeignCollectionField; 
import com.j256.ormlite.table.DatabaseTable; 
import java.util.Collection; 

@DatabaseTable 
public class User{ 

@DatabaseField(generatedId = true) 
private Long id; 

@DatabaseField 
private String username; 

@DatabaseField 
private String password; 

@ForeignCollectionField 
private ForeignCollection<Task> userTasks; 

public User(){ 

} 

public User(String username, String password) { 
    this.username = username; 
    this.password = password; 
} 

@Override 
public String toString() { 
    return " " + username + " " + password; 
} 

public String getUsername() { 
    return username; 
} 

public Long getId() { 
    return id; 
} 

public String getPassword() { 
    return password; 
} 

public Collection<Task> getUserTasks() { return userTasks; } 

} 

、私はこの試してみてください。

private void insertUsers(){ 
    User user1 = new User("Djavo", "666"); 
    User user2 = new User("Martel", "123"); 

    userDao.create(user1); 
    userDao.create(user2); 

    Task task1 = new Task(user1, "naslov1", "opis232", false); 
    Task task2 = new Task(user1, "naslov2", "opis3232", false); 

    taskDao.create(task1); 
    taskDao.create(task2); 

    Log.d("ORMLITEDEMO", "User1 and User2 and Task1 and Task2 CREATED!!!"); 
} 

エラーが発生しました:

によって引き起こさ:ます。java.sql.SQLException:データベースへの挿入に失敗しました:(????、、、)。INSERT INTO taskdescriptionuser_idtitledone)VALUES

私はAndroidでの初心者、特にORMLiteでのご利用をお勧めします。

答えて

0

私は問題が何であるかを知りました。 私はTaskクラスのTaskテーブルを変更したため、変更をコミットするためにデータベースを再作成する必要がありました。 だから、私のデータベースを落として、それをもう一度作成することでした。 これで動作します。

関連する問題